-2

Formatting the number works and cut unwanted decimals. Is there a reason the round() function give the same value passed by parameter?

number = 5.7821323121209e19
print(round(number,2)) # 5.7821323121209e19.
# Works.
print('%.3g' % number) # 5.78e19
print('{:.3g}'.format(number))  # 5.78e19
print(f'{number:.3g}')   # 5.78e19
Juan Alegría
  • 43
  • 1
  • 7

3 Answers3

2

Everything works correctly. 5.7821323121209e19 is actually 5.7821323121209 * (10 ** 19) so it is a number with only 0 after the comma.

Therefore there is nothing to round from mathematical point of view.

Magiczne
  • 1,586
  • 2
  • 15
  • 23
2

Looks like you want -17 instead of 2:

>>> round(5.7821323121208e19, -17)
5.78e+19
superb rain
  • 5,300
  • 2
  • 11
  • 25
1

round(number, 2) rounds to 2 decimal places (i.e. hundredths). 5.7821323121209e19 is much too big for that to have any effect. It is 57821323121209000000.

khelwood
  • 55,782
  • 14
  • 81
  • 108