tirana
1
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?
adamu
2
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
jerdew
3
> Decimal.new("10.3e44") |> Decimal.to_integer
1030000000000000000000000000000000000000000000
6 Likes
kip
4
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
adamu
5
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)