Python (3k) String formatting

leading and trailing 0's with a float

Page 1 of 1

6 Replies - 1545 Views - Last Post: 02 December 2010 - 09:56 AM Rate Topic: -----

#1 atraub   User is offline

  • Pythoneer
  • member icon

Reputation: 835
  • View blog
  • Posts: 2,267
  • Joined: 23-December 08

Python (3k) String formatting

Posted 27 November 2010 - 09:27 AM

I'm working on a project that involves printing out floats in Python. A given number may be anywhere from 14 to (1*10**-15). I'd like to print it out so that every number has 2 values to the left of the decimal and 15 to the right.

I've tried format(x, "02.15f") but I only get trailing 0's not leading ones. I know that one solution would be to do something like:

#assuming x is a POSITIVE float
x = (2 - len(str(x).split(".")[0])) * "0" + format(x,".15f")


would get the leading 0's, but I know there must be a better way! Any suggestions?

This post has been edited by atraub: 27 November 2010 - 12:29 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Python (3k) String formatting

#2 Guest_c.user*


Reputation:

Re: Python (3k) String formatting

Posted 27 November 2010 - 04:16 PM

>>> x = 5.7
>>> y = "{:018.15f}".format(x)
>>> y
'05.700000000000000'


Was This Post Helpful? 1

#3 atraub   User is offline

  • Pythoneer
  • member icon

Reputation: 835
  • View blog
  • Posts: 2,267
  • Joined: 23-December 08

Re: Python (3k) String formatting

Posted 27 November 2010 - 04:33 PM

Could you please explain your code?

"{:018.15f}" What does the :18 mean?
Was This Post Helpful? 0
  • +
  • -

#4 Guest_c.user*


Reputation:

Re: Python (3k) String formatting

Posted 27 November 2010 - 07:09 PM

18 is the width of the whole string, including left part, dot, right part
so, when you have fifteen characters after the dot, then you add 15 + 1 + 2
15 - fractional part
1 - dot
2 - integral part
Was This Post Helpful? 1

#5 atraub   User is offline

  • Pythoneer
  • member icon

Reputation: 835
  • View blog
  • Posts: 2,267
  • Joined: 23-December 08

Re: Python (3k) String formatting

Posted 27 November 2010 - 07:35 PM

After I stopped and looked at it, I kinda figured that was the case. It still is good that you took the time to explain it for anyone who finds this solution on google. It's noteworthy to mention that with negative numbers, the dash is included in the count, so it would need to be 1 higher.

Thanks for the response c.user!
Was This Post Helpful? 1
  • +
  • -

#6 Guest_c.user*


Reputation:

Re: Python (3k) String formatting

Posted 27 November 2010 - 07:50 PM

>>> x = 5.7
>>> y = "{1:0{0}.15f}".format(18 + (x < 0), x)
>>> y
'05.700000000000000'
>>> x = -5.7
>>> y = "{1:0{0}.15f}".format(18 + (x < 0), x)
>>> y
'-05.700000000000000'
>>>


Was This Post Helpful? 3

#7 atraub   User is offline

  • Pythoneer
  • member icon

Reputation: 835
  • View blog
  • Posts: 2,267
  • Joined: 23-December 08

Re: Python (3k) String formatting

Posted 02 December 2010 - 09:56 AM

Ahhh, C.user I am impressed by your clever use of booleans. Many Pythoners forget that booleans actually have a numerical value beneath the surface but as a C/C++ specialist, I bet that's old hat for you.

This post has been edited by atraub: 12 December 2010 - 09:34 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1