Doing a float division without installing additional gems?
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?
a = 50.0
b = 1048576.0
c = a/b
#=> 4.76837158203125e-005
sprintf("%.20f", c)
#> "0.00004768371582031250"
You can format a float using string formatting in Ruby like so:
irb> "%.019f" % c
=> "0.0000476837158203125"
精彩评论