简体   繁体   中英

How do I preserve leading zeros in Python integers for string formatting

I have what I believe to be a simple question. I have the following code and the output is not what I expect:

import socket
import time


r=031508.78
h=373309.9
z=0
mes='"TSRA %s %s %s\r\n"'%(r,h,z)
print mes

My problem is that r prints as '31508.78' and I want '031508.78'. How I get preserve the leading zero?

The 0 in front of the number is not important for python, because numbers have no leading 0s. You can use:

print '%09.2f'%r

I prefer the new string format mini language. I find it more intuitive.

r = 31508.78
h = 373309.9
z = 0

print '"TSRA {0: 011.3f} {1: 011.3f} {2}"\r\n'.format(r, h, z)
print '{0: 011.3f}\n{1: 011.3f}\n{2: 011.3f}'.format(r, h, z)

{0: 011.3f} 0: indicates the first item (r). 011 tells the formatter that there will be 11 characters including the decimal point and a place holder for a negative sign. By placing a 0 in front the formatter will pad the space with leading zeros. Finally, .3f indicates the precision and a fixed number of places after the decimal point.

Yields:

"TSRA  031508.780  373309.900 0"

 031508.780
 373309.900
 000000.000

I don't think there is any way to store a number with a leading zero in python. This leaves you with two ways:

1) Make ra string ie r="031408.78"

I think this is the best method for you as you can do all the arithmetic you want to do (using the implicit typecasting of python) and see the number appended with a 0 wherever you use it.

2) If you simply want to print the number with leading zeros consider using this

print "%09.2f' % r  #for this you need the length of the number

For Python, there is no difference in the following things:

31508.78
031508.78
0000000031508.78
31508.7800000000000
0000000000000031508.7800000000000

Those are all valid representations of the exact same number, which is canonically represented by 31508.78 , which is essentially just a convention. Different representations are possible too, and actually, the number is represented in a way that's described by the IEEE-754 standard .

So, when you want that leading zero, you have to tell Python explicitely to put it there because it simply doesn't know about it. You just used one of many valid representations to specify that one number which default string representation does not include leading zeroes.

The best way to do this is using string formatting, which comes in two flavors. The old style flavor, using the % operator, and the newer str.format function:

>>> '%09.2f' % 31508.78
'031508.78'
>>> '{:09.2f}'.format(31508.78)
'031508.78'

Or, if you don't actually want a number, you could also just specify the number as a string to begin with, which of course keeps the leading zero.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM