views:

99

answers:

2

Let's say I have these two floats:

a = 50.0
b = 1048576.0
c = a/b

By printing c, I get this:

4.76837158203125e-005

Doing the division with calc.exe gives me the result 0.0000476837158203125 . Is there any way of achieving the same thing with Ruby without installing any additional gem?

+3  A: 
a = 50.0
b = 1048576.0
c = a/b
#=> 4.76837158203125e-005
sprintf("%.20f", c)
#> "0.00004768371582031250"
fl00r
+2  A: 

You can format a float using string formatting in Ruby like so:

irb> "%.019f" % c
=> "0.0000476837158203125"
maerics
won't be enough `"%.19f" % c`? why do you use additional zero?
fl00r
yes, you are right, I just used the zero padding from habit of printing integers but it is not necessary.
maerics