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?
If you want to avoid scientific notation for a given number or a series of numbers, you can use the format() function by passing scientific = FALSE as an argument.
Convert scientific notation to decimal formDetermine the exponent, n , on the factor 10 . Move the decimal n places, adding zeros if needed. If the exponent is positive, move the decimal point n places to the right. If the exponent is negative, move the decimal point |n| places to the left.
To set the use of scientific notation in your entire R session, you can use the scipen option. From the documentation ( ? options ): 'scipen': integer.
decimal h = Convert. ToDecimal("2.09550901805872E-05"); decimal h2 = Decimal. Parse( "2.09550901805872E-05", System.
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.
Just for convenience you can also control number of digits after decimal point. So do:
x = 1.234598
"%.3E" % x=> "1.235E+00"
Another neat thing you can do is pad with space from left like this:
x = 1.234
"%10.3E" % x => " 1.234E+00"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With