In Python, I am trying to convert floating point numbers to strings such that the string is exactly 12 characters long: the first character is a space and the remaining characters can be filled with the digits (and decimal point if needs be) of the number to be converted to a string. Also, the numbers need to be expressed in decimal form (no scientific notation). I am working inside a fixed format in a particular file; hence the exact parameters stated above. It can be assumed that all numbers I'm working with are less than 1e12, i.e., that all numbers can be expressed with
I am using
s = " %11f" % number
Most of the floating point numbers convert to a string fitting my formatting parameters just fine; however, some of the larger numbers don't. For example,
print " %11f" % 325918.166005444
gives 325918.166005
. This takes up 13 characters, not 11.
Why is my code doing this, and how can I fix this? I'd like to keep as much precision as possible (i.e., simply truncating the fractional portion of the number is not a good enough solution).
If Python version is important, I'm using 2.7.