How can I round large numbers in Python, for example from 1.99206595e20 to 1.99e20?
Because I tried round()
and it did not work.
How can I round large numbers in Python, for example from 1.99206595e20 to 1.99e20?
Because I tried round()
and it did not work.
You need to pass the second argument to the round
function to specify how many digits you want to round off the number to. Positive values means, you want to round after decimal point, whereas negative means before the decimal point.
Therefore, you can use.
round(float('1.99206595e20'), -18)
Which outputs:
1.99e+20
This has rounded the initial values to the 18 digits left of the decimal points.
Your example of 1.99206595e20 actually looks like 19920659500000000000.
That means, if you want to round it to the first 3 digits, they're going to be 17 places before the decimal place.
print(round(1.99206595e20, -18))
Outputs 1.99e+20.