Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to format a DOUBLE to print only two decimals in Eiffel?

Tags:

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.

like image 834
elviejo79 Avatar asked Dec 31 '25 22:12

elviejo79


1 Answers

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

like image 90
Jocelyn Avatar answered Jan 06 '26 10:01

Jocelyn



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!