planarian
planarian

Reputation: 2151

reconstituting an object from console output

Is there a way to convert console output like "#<BigDecimal:6999660,'0.1E3',9(36)>" back into the original object? The integer form of this number is 100, but I have no way of knowing that when I see it returned by RSpec (for example).

Upvotes: 3

Views: 398

Answers (1)

Bruno E.
Bruno E.

Reputation: 1424

Manually you can just call BigDecimal.new('0.1E3').to_s in a rails console and get the number: => "100.0".

In that example the E notation is really simple, you just have to move the . by the number after E positions. If it's > 0 move it to the right, if it's smaller < 0 move it to the left. See https://en.wikipedia.org/wiki/Scientific_notation#E_notation for a more detailed explanation.

0.1E3 == 1.0E2 == 10.0E1 == 100.0E0 == 100.0

Upvotes: 2

Related Questions