What is the canonical way of converting a string storing a number in scientific notation into an integer?
"1e6"
1000000
As for the reverse process, converting integer to string in scientific notation I understand I can use @sprintf
macro. If one knows the exact format to achieve exactly the reverse process - so small e
and no extra trailing .00
zeros (like 1.00e6
), or leading zeros (like 1e08
) - I will appreciate if it will be included for completeness.
The conversion from string to integer can be achieved via floats like this:
julia> Int(parse(Float64, "1e6"))
1000000
if you know that the number will fit into Int64
or like this
julia> BigInt(parse(BigFloat, "1e6"))
1000000
for larger numbers.
For the reverse process the default in @sprintf
would be the following:
julia> @sprintf("%.0e", 1_000_000)
"1e+06"
However, you get +
after e
and at least two digits are displayed in the exponent (both features are a standard to expect across different languages when you do such a conversion). Also note that this process will lead to rounding, e.g.:
julia> @sprintf("%.0e", 1_000_001)
"1e+06"