如何格式化DOUBLE以在Eiffel中仅打印两个小数? [英] How to format a DOUBLE to print only two decimals in Eiffel?
问题描述
在埃菲尔铁塔中,您如何制作数字?
In eiffel how do you make it so that the number.
118.1999999999999
打印到:
118.20
换句话说,这只是printf的问题,但似乎没有办法在Eiffel中轻松地做到这一点.
In other language is simply a matter of printf but there seems no to be a way to do that easily in Eiffel.
推荐答案
您应该使用FORMAT_DOUBLE类
You should use the class 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 我还没有亲自使用过它们,但这可能会满足您的需求.
There is also a set of classes to mimic "printf" , you can find them at http://www.amalasoft.com/downloads.htm I haven't used them myself, but that might address your needs.
这是使用ECMA Eiffel(我不确定来自先前响应的位置,但是DOUBLE没有这样的功能"to_string_format".DOUBLE是REAL_64的旧名称
This is using ECMA Eiffel (I am not sure where comes from the previous response, but DOUBLE does not have such function `to_string_format'. And DOUBLE is the old name for REAL_64
这篇关于如何格式化DOUBLE以在Eiffel中仅打印两个小数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!