str.format rounding to the left of the decimal - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: str.format rounding to the left of the decimal (/thread-17061.html) |
str.format rounding to the left of the decimal - ClassicalSoul - Mar-26-2019 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) >>> 50000000Moreover, is there somewhere I could have accessed this information, like the help function? Thanks RE: str.format rounding to the left of the decimal - ichabod801 - Mar-26-2019 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. RE: str.format rounding to the left of the decimal - perfringo - Mar-27-2019 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' |