0

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.

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
  • 1
    Do you need a rounded float value, or just a string representation? Something like `f"{1.99206595e20:.2e}"` would work for the latter. – Brian61354270 Jul 16 '23 at 17:31

2 Answers2

1

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.

Zero
  • 1,807
  • 1
  • 6
  • 17
1

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.

  • "Outputs 1.88e+20." -- either your computer is very broken, or you did a rather poor job proofreading those few lines before hitting the submit button... (although given how blatant a duplicate the question is, it probably shouldn't have been answered in the first place). – Dan Mašek Jul 16 '23 at 17:40
  • @Dan Mašek alright I'm sorry? It was a typo you don't need to let out all of your built up hatred onto it. – felix moeran Jul 16 '23 at 17:48