Doing a floating unit without installing additional gems?

Let's say I have these two floats:

a = 50.0
b = 1048576.0
c = a/b

      

c

When I print it out , I get the following:

4.76837158203125e-005

      

Doing division with calc.exe

gives me the result 0.0000476837158203125. Is there a way to achieve the same with Ruby without installing any additional gems?

+2


a source to share


2 answers


a = 50.0
b = 1048576.0
c = a/b
#=> 4.76837158203125e-005
sprintf("%.20f", c)
#> "0.00004768371582031250"

      



+5


a source


You can format floats using Ruby string formatting like:



irb> "%.019f" % c
=> "0.0000476837158203125"

      

+2


a source







All Articles