【发布时间】:2019-05-18 00:23:59
【问题描述】:
考虑以下数字:
1000.10
1000.11
1000.113
我想让这些在 python 中打印出来:
1,000.10
1,000.11
1,000.11
以下转换几乎可以做到这一点,除了当小数点右侧的第二个数字为零时,零会被省略,因此数字无法正确排列。
这是我的尝试:
for n in [1000.10, 1000.11, 1000.112]:
nf = '%.2f' %n # nf is a 2 digit decimal number, but a string
nff = float(nf) # nff is a float which the next transformation needs
n_comma = f'{nff:,}' # this puts the commas in
print('%10s' %n_comma)
1,000.1
1,000.11
1,000.11
有没有办法避免在第一个数字中省略结尾的零?
【问题讨论】:
标签: python floating-point type-conversion