Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 str.format rounding to the left of the decimal
#1
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
Quote
#2
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

Quote
#3
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'
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  price + tax rounding mlieqo 11 388 Sep-21-2019, 04:53 PM
Last Post: mlieqo
  rounding floats to a number of bits Skaperen 2 133 Sep-13-2019, 04:37 AM
Last Post: Skaperen
  Why is left mouse click not working? yeto 3 264 Jul-15-2019, 05:23 AM
Last Post: Yoriz
  testing for Decimal w/o importing decimal every time Skaperen 7 628 May-06-2019, 10:23 PM
Last Post: Skaperen
  Move a character left or right in a file. DreamingInsanity 4 383 Mar-21-2019, 07:52 PM
Last Post: DreamingInsanity
  Rounding/Truncating Numbers Trinx 3 440 Jan-16-2019, 10:54 PM
Last Post: Trinx
  Weird rounding PythonZenon 8 729 Oct-12-2018, 09:22 PM
Last Post: volcano63
  I'm dividing large numbers of about 25 million digits I need to retain decimal format Pleiades 2 718 Apr-26-2018, 07:50 AM
Last Post: Pleiades
  Controlling trailing zeros with rounding? RedSkeleton007 1 7,414 Jan-25-2018, 07:23 AM
Last Post: j.crater
  Can't change Right to left id spawning/ movement to top to bottom Kingrocket10 3 1,146 Dec-14-2017, 11:05 PM
Last Post: Larz60+

Forum Jump:


Users browsing this thread: 1 Guest(s)