'%03.1f' works (1 could be any number, or empty string):
>>> "%06.2f"%3.3
'003.30'
>>> "%04.f"%3.2
'0003'
Note that the field width includes the decimal and fractional digits.
Answer from Jonathan Graehl on Stack Overflow'%03.1f' works (1 could be any number, or empty string):
>>> "%06.2f"%3.3
'003.30'
>>> "%04.f"%3.2
'0003'
Note that the field width includes the decimal and fractional digits.
Alternatively, if you want to use .format:
{:6.1f}
↑ ↑
| |
# digits to pad | | # of decimal places to display
Copy paste: {:6.1f}
The 6 above includes digits to the left of the decimal, the decimal marker, and the digits to the right of the decimal.
Examples of usage:
'{:6.2f}'.format(4.3)
Out[1]: ' 4.30'
f'{4.3:06.2f}'
Out[2]: '004.30'
'{:06.2f}'.format(4.3)
Out[3]: '004.30'
You can use the format specifiers, like this
>>> "{:0>8.4f}".format(2.02)
'002.0200'
>>> print("{:0>8.4f}".format(2.02))
002.0200
>>>
Here, 8 represents the total width, .4 represents the precision. And 0> means that the string has to be right aligned and filled with 0 from the left.
You can do it using both old and new formatting method for strings::
In [9]: "%08.4f" %(2.02)
Out[9]: '002.0200'
In [10]: "{:08.4f}".format(2.02)
Out[10]: '002.0200'
To format a floating point number with a given precision you should use the 'f' specifier. Without it the value is formatted as a string.
>>> '{:.4f}'.format(4.1)
'4.1000
You can also specify the minimum width of the complete output and the fill character:
>>> '{:07.4f}'.format(4.1)
'04.1000'
Here the output is padded on the left with zeros to be at least 7 characters.
'{:5.5f}'.format(4.1)
The f in the format string makes the difference here. You can read more about the formatters in here, important for this task is
'f' Fixed point. Displays the number as a fixed-point number. The default precision is 6.
But you wanted just 4 digits after the comma, so better is
>>> '{:.4f}'.format(4.1)
'4.1000'
numbers = [23.23, 0.1233, 1.0, 4.223, 9887.2]
for x in numbers:
print("{:10.4f}".format(x))
prints
23.2300
0.1233
1.0000
4.2230
9887.2000
The format specifier inside the curly braces follows the Python format string syntax. Specifically, in this case, it consists of the following parts:
- The empty string before the colon means "take the next provided argument to
format()" – in this case thexas the only argument. - The
10.4fpart after the colon is the format specification. - The
fdenotes fixed-point notation. - The
10is the total width of the field being printed, lefted-padded by spaces. - The
4is the number of digits after the decimal point.
It has been a few years since this was answered, but as of Python 3.6 (PEP498) you could use the new f-strings:
numbers = [23.23, 0.123334987, 1, 4.223, 9887.2]
for number in numbers:
print(f'{number:9.4f}')
Prints:
23.2300
0.1233
1.0000
4.2230
9887.2000
To pad strings:
>>> print('4'.zfill(3))
004
>>> print('-4'.zfill(3))
-04
To pad numbers:
>>> n = 4
>>> print(f'{n:03}') # Preferred method, python >= 3.6
004
>>> print('%03d' % n)
004
>>> print(format(n, '03')) # python >= 2.6
004
>>> print('{0:03d}'.format(n)) # python >= 2.6 + python 3
004
>>> print('{foo:03d}'.format(foo=n)) # python >= 2.6 + python 3
004
>>> print('{:03d}'.format(n)) # python >= 2.7 + python3
004
String formatting documentation.
Just use the rjust method of the string object.
This example creates a 10-character length string, padding as necessary:
>>> s = 'test'
>>> s.rjust(10, '0')
>>> '000000test'
You need a f after 3:
In [19]: "{:.3f}".format(0.12)
Out[19]: '0.120'
In case of floats if you don't specify any type then it uses 'g' by default.
In [27]: "{:.20}".format(0.12)
Out[27]: '0.11999999999999999556'
In [28]: "{:.20g}".format(0.12)
Out[28]: '0.11999999999999999556'
Documentation here.
You can do it with a format string
print('%.3f' % 0.12)
Format floating point number with padding zeros:
Copynumbers = [0.0, 0.1, 0.29, 1.278, 59.0, 99.9]
for x in numbers:
print("{:05.2f}".format(x).replace(".","_"))
Which prints:
Copy00_00
00_10
00_29
01_28
59_00
99_90
The :05 means 'pad with zeros using five places' and .2f means 'force show 2 digits after the decimal point'. For background on formatting numbers see: https://pyformat.info/#number.
Hat-tip: How to format a floating number to fixed width in Python
Your original solution works if you calculate heightDec this way:
CopyheightDec = int(round((height - heightWhole)*100, 0))
First multiply by 100 and then round and convert to int.