如何在Python中将浮点数格式化为固定宽度

问题:如何在Python中将浮点数格式化为固定宽度

如何按照以下要求将浮点数格式化为固定宽度:

  1. 如果n <1,则前导零
  2. 添加尾随的十进制零以填充固定宽度
  3. 截断超出固定宽度的十进制数字
  4. 对齐所有小数点

例如:

% formatter something like '{:06}'
numbers = [23.23, 0.123334987, 1, 4.223, 9887.2]

for number in numbers:
    print formatter.format(number)

输出会像

  23.2300
   0.1233
   1.0000
   4.2230
9887.2000

How do I format a floating number to a fixed width with the following requirements:

  1. Leading zero if n < 1
  2. Add trailing decimal zero(s) to fill up fixed width
  3. Truncate decimal digits past fixed width
  4. Align all decimal points

For example:

% formatter something like '{:06}'
numbers = [23.23, 0.123334987, 1, 4.223, 9887.2]

for number in numbers:
    print formatter.format(number)

The output would be like

  23.2300
   0.1233
   1.0000
   4.2230
9887.2000

回答 0

for x in numbers:
    print "{:10.4f}".format(x)

版画

   23.2300
    0.1233
    1.0000
    4.2230
 9887.2000

花括号内的格式说明符遵循Python格式字符串语法。具体来说,在这种情况下,它由以下部分组成:

  • 空字符串冒号前的手段“采取下一个提供参数format()” -在这种情况下,x作为唯一的参数。
  • 10.4f冒号之后的部分是格式规范
  • f表示定点表示法。
  • 10是该领域的总宽度被印刷,用空格lefted-填充。
  • 4是小数点后的位数。
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 the x as the only argument.
  • The 10.4f part after the colon is the format specification.
  • The f denotes fixed-point notation.
  • The 10 is the total width of the field being printed, lefted-padded by spaces.
  • The 4 is the number of digits after the decimal point.

回答 1

自从这个答案问了已经好几年了,但是从Python 3.6(PEP498)开始,您可以使用新的f-strings

numbers = [23.23, 0.123334987, 1, 4.223, 9887.2]

for number in numbers:
    print(f'{number:9.4f}')

印刷品:

  23.2300
   0.1233
   1.0000
   4.2230
9887.2000

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

回答 2

在python3中,以下工作原理:

>>> v=10.4
>>> print('% 6.2f' % v)
  10.40
>>> print('% 12.1f' % v)
        10.4
>>> print('%012.1f' % v)
0000000010.4

In python3 the following works:

>>> v=10.4
>>> print('% 6.2f' % v)
  10.40
>>> print('% 12.1f' % v)
        10.4
>>> print('%012.1f' % v)
0000000010.4

回答 3

请参阅Python 3.x 格式字符串语法

IDLE 3.5.1   
numbers = ['23.23', '.1233', '1', '4.223', '9887.2']

for x in numbers:  
    print('{0: >#016.4f}'. format(float(x)))  

     23.2300
      0.1233
      1.0000
      4.2230
   9887.2000

See Python 3.x format string syntax:

IDLE 3.5.1   
numbers = ['23.23', '.1233', '1', '4.223', '9887.2']

for x in numbers:  
    print('{0: >#016.4f}'. format(float(x)))  

     23.2300
      0.1233
      1.0000
      4.2230
   9887.2000

回答 4

您也可以将零填充为零。例如,如果您number要有9个字符的长度,请用零左填充,请使用:

print('{:09.3f}'.format(number))

因此,如果为number = 4.656,则输出为:00004.656

对于您的示例,输出将如下所示:

numbers  = [23.2300, 0.1233, 1.0000, 4.2230, 9887.2000]
for x in numbers: 
    print('{:010.4f}'.format(x))

印刷品:

00023.2300
00000.1233
00001.0000
00004.2230
09887.2000

一个可能有用的示例是当您要按字母顺序正确列出文件名时。我注意到在某些linux系统中,数字是:1,10,11,.. 2,20,21,…

因此,如果要在文件名中强制执行必要的数字顺序,则需要在键盘上填充适当数量的零。

You can also left pad with zeros. For example if you want number to have 9 characters length, left padded with zeros use:

print('{:09.3f}'.format(number))

Thus, if number = 4.656, the output is: 00004.656

For your example the output will look like this:

numbers  = [23.2300, 0.1233, 1.0000, 4.2230, 9887.2000]
for x in numbers: 
    print('{:010.4f}'.format(x))

prints:

00023.2300
00000.1233
00001.0000
00004.2230
09887.2000

One example where this may be useful is when you want to properly list filenames in alphabetical order. I noticed in some linux systems, the number is: 1,10,11,..2,20,21,…

Thus if you want to enforce the necessary numeric order in filenames, you need to left pad with the appropriate number of zeros.


回答 5

在Python 3中。

GPA = 2.5
print(" %6.1f " % GPA)

6.1f点之后手段1个数字显示,如果你,你应该只点打印后2位%6.2f,使得%6.3f3位点后打印。

In Python 3.

GPA = 2.5
print(" %6.1f " % GPA)

6.1f means after the dots 1 digits show if you print 2 digits after the dots you should only %6.2f such that %6.3f 3 digits print after the point.