如何在 Eiffel 中格式化 DOUBLE 以仅打印两位小数?

发布于 2024-09-13 06:52:56 字数 169 浏览 6 评论 0原文

在埃菲尔铁塔中,你如何做到这一点。

118.1999999999999

打印到:

118.20 

在其他语言中只是 printf 的问题,但在 Eiffel 中似乎没有办法轻松做到这一点。

In eiffel how do you make it so that the number.

118.1999999999999

prints to:

118.20 

In other language is simply a matter of printf but there seems no to be a way to do that easily in Eiffel.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

温柔一刀 2024-09-20 06:52:56

您应该使用类 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 Eiffel (我不确定之前的响应来自哪里,但 DOUBLE 没有这样的函数“to_string_format”。DOUBLE 是 REAL_64 的旧名称

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     ]

And so on ...

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.

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

-柠檬树下少年和吉他 2024-09-20 06:52:56

例如:


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

For example:


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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文