1

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?

gen_Eric
  • 223,194
  • 41
  • 299
  • 337

1 Answers1

1

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
SparkAndShine
  • 17,001
  • 22
  • 90
  • 134