Python Forum

Full Version: str.format rounding to the left of the decimal
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi, I was wondering how I could make string.format round to the left of the decimal.


>>> x = 4. 542412343
>>> '{0} rounded to {1} decimals is {2: .1f}'.format(x, 4, x) 
>>> 4.5
>>> x = 52137809
>>> '{0} rounded to {1} decimals is {2: ???}'.format(x, 4, x) 
>>> 50000000
Moreover, is there somewhere I could have accessed this information, like the help function?

Thanks
I do not believe that is possible. You would have to do some calculation on the number before string formatting. You can see the full format method syntax here.
Probably you should round before displaying:

>>> x = 52137809
>>> '{0} rounded to tens of millions is {1}'.format(x, round(x, -7))  # format method
'52137809 rounded to tens of millions is 50000000'
>>> f'{x} rounded to tens of millions is {round(x, -7)}'              # f-string
'52137809 rounded to tens of millions is 50000000'