2014-09-26 80 views
6

你如何在計劃或球拍中獲得printf %6.2f,就像在C中一樣?printf%6.2f在方案或球拍?

現在我所擁有的是printf "The area of the disk is ~s\n" (- d1 d2),但我無法將輸出格式化爲特定的浮點格式。

感謝

回答

4

爲了得到一個行爲更接近C中的printf()功能使用由SRFI-48提供的format過程中,像這樣:

(require srfi/48) 
(format "The area of the disk is ~6,2F~%" (- d1 d2)) 

一個更詳細的替代方案是使用球拍的內置~r程序,如@stchang所示:

(string-append 
"The area of the disk is " 
(~r (- d1 d2) #:min-width 6 #:precision '(= 2)) 
"\n") 
+1

而在[tag:Scheme]中,您只需在進口中包含'(srfi:48)'而不是'require'形式。 – Sylwester 2014-09-26 20:52:20

+1

也許我應該就此開始一個單獨的問題,但是,「srfi/48」如何成爲一個好名字?在其他語言中,您需要「文本」或「格式」。 – 2015-05-30 16:19:09

4

球拍有~r

您可能會想要提供#:min-width#:precision參數。