2014-09-26 79 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参数。