'Python - %n.nf format
I'd like to understand the first argument %n for the format command %n.nf.
IE:
print("number is %1.3f" %(1.1000000000))
number is 1.100
Ok, I've understand the "3f" is the total of total of digits after decimal point.
but what about the fist argument?
another example:
print("number is %3.3f" %(1111.1000000000))
number is 1111.100
Argument say 3, but return 4 digits.
or
print("number is %10.3f" %(1111.1000000000))
number is 1111.100
Even saying 10, not fill with 0.
Solution 1:[1]
The first argument in %n.nf is the minimum field width. It doesn't appear to do anything in your first example (%1.3f) because the output is already wider than the minimum specified.
It left-pads with spaces by default, so your second example does work. Just add a leading 0 to get it to pad with zeroes instead of spaces.
>>> print("number is %010.3f" %(1111.1000000000))
number is 001111.100
See printf-style String Formatting for more details. (As mentioned in the comments, this is the old style of string formatting. Ctrl-F on that page to see newer methods.)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Bill the Lizard |
