在艾菲爾,你是如何使它的數量。如何格式化DOUBLE在埃菲爾中只打印兩位小數?
118.1999999999999
打印到:
118.20
在其他語言的printf簡單的問題,但似乎沒有成爲一個辦法做到這一點很容易在埃菲爾鐵塔。
在艾菲爾,你是如何使它的數量。如何格式化DOUBLE在埃菲爾中只打印兩位小數?
118.1999999999999
打印到:
118.20
在其他語言的printf簡單的問題,但似乎沒有成爲一個辦法做到這一點很容易在埃菲爾鐵塔。
例如:
class DOBLE
creation
make
feature
make is
local
n: DOUBLE
output: STRING
do
n := 118.1999999999999
output := n.to_string_format(2) -- 2 digits in fractionnal part
std_output.put_string(output + "%N")
end
end
您應該使用類FORMAT_DOUBLE
local
fd: FORMAT_DOUBLE
do
create fd.make (5, 3)
print (fd.formatted ({REAL_64} 12345.6789)) --> "12345.679"
print (fd.formatted ({REAL_64} 12345.6)) --> "12345.600"
print (fd.formatted ({REAL_64} 0.6)) --> "0.600"
create fd.make (10, 2)
fd.right_justify
print (fd.formatted ({REAL_64} 1.678)) --> " 1.68"
create fd.make (20, 3)
fd.right_justify
print ("[" + fd.formatted ({REAL_64} 12345.6789) + "]%N") --> [ 12345.679]
fd.left_justify
print ("[" + fd.formatted ({REAL_64} 12345.6789) + "]%N") --> [12345.679 ]
fd.center_justify
print ("[" + fd.formatted ({REAL_64} 12345.6789) + "]%N") --> [ 12345.679 ]
等等......
還有一組類模仿的 「printf」你可以在http://www.amalasoft.com/downloads.htm 找到他們我沒有使用他們自己,但這可能會滿足您的需求。
這是通過使用ECMA艾菲爾(我不知道在哪裏,從之前的響應來了,但雙不具備這樣的功能`to_string_format」和DOUBLE是REAL_64
老字號