Python:如何设置固定宽度的数字格式?

Python:如何设置固定宽度的数字格式?,python,string,numbers,fixed-width,Python,String,Numbers,Fixed Width,比方说 numbers = [ 0.7653, 10.2, 100.2325, 500.9874 ] 我想通过改变小数位数来输出固定宽度的数字,得到如下输出: 0.7653 10.200 100.23 500.98 def to_fixed_width(n, max_width, allow_overflow = True, do_round = True): if do_round: for i in range(max_width - 2, -1, -1):

比方说

numbers = [ 0.7653, 10.2, 100.2325, 500.9874 ]
我想通过改变小数位数来输出固定宽度的数字,得到如下输出:

0.7653
10.200
100.23
500.98
def to_fixed_width(n, max_width, allow_overflow = True, do_round = True):
    if do_round:
        for i in range(max_width - 2, -1, -1):
            str0 = '{:.{}f}'.format(n, i)
            if len(str0) <= max_width:
                break
    else:
        str0 = '{:.42f}'.format(n)
        int_part_len = str0.index('.')
        if int_part_len <= max_width - 2:
            str0 = str0[:max_width]
        else:
            str0 = str0[:int_part_len]
    if (not allow_overflow) and (len(str0) > max_width):
        raise OverflowError("Impossible to represent in fixed-width non-scientific format")
    return str0
有没有一个简单的方法可以做到这一点?我一直在尝试各种
%f
%d
配置,但运气不佳。

组合了两个/调用:

或:

或者,您可以使用:


不幸的是,这个问题没有现成的解决方案。此外,使用字符串切片的解决方案不能充分处理舍入和溢出

因此,似乎必须编写这样一个自己的函数:

0.7653
10.200
100.23
500.98
def to_fixed_width(n, max_width, allow_overflow = True, do_round = True):
    if do_round:
        for i in range(max_width - 2, -1, -1):
            str0 = '{:.{}f}'.format(n, i)
            if len(str0) <= max_width:
                break
    else:
        str0 = '{:.42f}'.format(n)
        int_part_len = str0.index('.')
        if int_part_len <= max_width - 2:
            str0 = str0[:max_width]
        else:
            str0 = str0[:int_part_len]
    if (not allow_overflow) and (len(str0) > max_width):
        raise OverflowError("Impossible to represent in fixed-width non-scientific format")
    return str0
更多示例:

>>> to_fixed_width(-0.3, 6)
'-0.300'
>>> to_fixed_width(0.000001, 6)
'0.0000'
>>> to_fixed_width(999.99, 6)
'999.99'
>>> to_fixed_width(999.999, 6)
'1000.0'
>>> to_fixed_width(1000.4499, 6)
'1000.4'
>>> to_fixed_width(1000.4499, 6, do_round = False)
'1000.4'
>>> to_fixed_width(12345.6, 6)
'12346'
>>> to_fixed_width(1234567, 6)
'1234567'
>>> to_fixed_width(1234567, 6, allow_overflow = False)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 15, in to_fixed_width
OverflowError: Impossible to represent in fixed-width non-scientific format
>>> to_fixed_width(float('nan'), 6)
'nan'
>>至固定宽度(-0.3,6)
'-0.300'
>>>至固定宽度(0.000001,6)
'0.0000'
>>>至固定宽度(999.99,6)
'999.99'
>>>至固定宽度(999.999,6)
'1000.0'
>>>至固定宽度(1000.4499,6)
'1000.4'
>>>至固定宽度(1000.4499,6,不圆=假)
'1000.4'
>>>至固定宽度(12345.6,6)
'12346'
>>>至固定宽度(1234567,6)
'1234567'
>>>至固定宽度(1234567,6,允许溢出=假)
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
文件“”,第15行,固定宽度
溢出错误:无法以固定宽度的非科学格式表示
>>>至固定宽度(浮动('nan'),6)
“楠”

谢谢!这正是我所需要的。请注意,这些解决方案不会将最后一个小数点舍入。最后一个小数点不应该舍入到500.99吗?这是一个惊人的通用函数,谢谢@Roman!我专门需要这个案例(allow_overflow=False,do_round=True),所以我使用了修改后的函数版本。
>>> to_fixed_width(0.7653, 6)
'0.7653'
>>> to_fixed_width(10.2, 6)
'10.200'
>>> to_fixed_width(100.2325, 6)
'100.23'
>>> to_fixed_width(500.9874, 6)
'500.99'
>>> to_fixed_width(500.9874, 6, do_round = False)
'500.98'
>>> to_fixed_width(-0.3, 6)
'-0.300'
>>> to_fixed_width(0.000001, 6)
'0.0000'
>>> to_fixed_width(999.99, 6)
'999.99'
>>> to_fixed_width(999.999, 6)
'1000.0'
>>> to_fixed_width(1000.4499, 6)
'1000.4'
>>> to_fixed_width(1000.4499, 6, do_round = False)
'1000.4'
>>> to_fixed_width(12345.6, 6)
'12346'
>>> to_fixed_width(1234567, 6)
'1234567'
>>> to_fixed_width(1234567, 6, allow_overflow = False)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 15, in to_fixed_width
OverflowError: Impossible to represent in fixed-width non-scientific format
>>> to_fixed_width(float('nan'), 6)
'nan'