How to convert a number in the E-notation -- "5e+24" -- into a decimal Integer?

I have an number returned from an external source in the format

5e+24

which is called scientific notation, as I’ve been told.

I want to convert it into an decimal Integer. Not into a float, but an integer.

In this case, it’d be 5000000000000000000000000

How to do this?

You could split the string and multiply it yourself:

[a, b] = String.split("5e+24", "e+")
String.to_integer(a) * 10 ** String.to_integer(b)
2 Likes
> Decimal.new("10.3e44") |> Decimal.to_integer
1030000000000000000000000000000000000000000000
6 Likes

To account for a possible negative exponent:

[a, b] = String.split("5e+24", "e")
String.to_integer(a) * 10 ** String.to_integer(b)
1 Like

Right, but that will also produce non-integers.

2 Likes

Either what @jerdew posted or, if you want to handle errors somewhat, be a bit more explicit with:

{dec, ""} = Decimal.parse("5e+24")
Decimal.to_integer(dec)