tags:

views:

208

answers:

1

If I have an extremely long floating point number in Ruby such as:

 x = 123456789012345.to_f

when it is displayed, say, via to_s, it appears in scientific notation:

 "1.23456789012345e+14"

Is there any way to suppress the formatting in scientific notation, or on the other side of the coin, force it for extremely short floating point numbers?

+4  A: 

You can do all sorts of things using the % operator. For example:

x = 123456789012345.to_f
"%f" % x  # => "123456789012345.000000"

y = 1.23
"%E" % y # => "1.230000E+000"

The various options are the same as for the sprintf function.

Pesto