How do I string format, either %
or .format()
, a float to round and display to the 10s or 100s place?
Like 4552.33 to 4550 to 10s place or 4600 to 100s?
How do I string format, either %
or .format()
, a float to round and display to the 10s or 100s place?
Like 4552.33 to 4550 to 10s place or 4600 to 100s?
Use the built-in function round
,
>>> import math
>>> f = 4552.33
>>> int(round(f, -int(math.log10(10))))
4550
>>> int(round(f, -int(math.log10(100))))
4600