-2

I use Ruby. I have str="0.2592585e7" I need to convert this to exp notation and get the same result like x=0.2592585e7 When I do this via format I get: irb(main):064:0> format("%0.6e", str.to_f) => "2.592585e+06" Any idea how to not get decimal before point ?

Oleh
  • 1
  • 1
    "I need to convert this to exp notation and get the same result like x=0.2592585e7" - huh? It's already a valid number string in scientific notation. Just do `str.to_f` to get the number. – Sergio Tulentsev Jun 26 '18 at 17:21
  • In your case I get - irb(main):002:0> result = str.to_f => 2592585.0 But I need 0.2592585e7 – Oleh Jun 27 '18 at 08:14
  • It's the same number. For all practical purposes, there is no difference. Mind explaining _why_ you think you need that? – Sergio Tulentsev Jun 27 '18 at 08:25

1 Answers1

1

If I understood you correctly, you can convert a string to float like this:

str = "0.2592585e7"

puts result = str.to_f / 10000000

or

str = "0.2592585e7"
result =  str.to_f

puts result.to_i

To get an integer

PavelB
  • 67
  • 2