如何跳过 Python 的 f 字符串中小数点的尾随零?
How to skip trailing zeroes from decimal in Python's f-strings?
我想在我的 f 字符串中设置一个精度,但我不希望小数部分中的尾随零。
i = 1002
print(f'{i/1000:.2f}') # 1.00 but this must be 1
i = 1009
print(f'{i/1000:.2f}') # 1.01, this is correct
第一个 print
必须是 1
,第二个 print
是我预期的行为 1.01
。
我尝试了 :g
但它适用于第一个 print
但失败了第二个。
i = 1000
print(f'{i/1000:.2g}') # 1, this is correct but
i = 1009
print(f'{i/1000:.2g}') # 1, but this must be 1.01
我尝试过的一种方法是f'{i/1000:.2f}'.strip('.0')
,但我想知道是否有更好的方法。
编辑:
在我的实际代码中,如果 i
是 100000
,那么分母也将是 100000(i
顺序中的最小数字),换句话说,我的代码中的分母将是始终使 i// 分母始终产生一个数字。
如果你的字符串只有浮点数,那么你可以使用你现在拥有的str.rstrip()
(instead of str.strip()
)。此外,您需要首先使用 '0'
对其进行链式调用,然后使用 '.'
(例如 .rstrip('0').rstrip('.')
)来处理带有尾随零的整数,例如 10000
.
但是,如果您的字符串中可以有其他字符,并且您只想去除数字 0
,那么您可以使用嵌套的 f-string 作为:
>>> f"{f'{1002/1000:.2f}'.rstrip('0').rstrip('.')} number"
'1 number'
>>> f"{f'{1009/1000:.2f}'.rstrip('0').rstrip('.')} number"
'1.01 number'
>>> f"{f'{1000:.2f}'.rstrip('0').rstrip('.')} number"
'1000 number'
您必须使用两种不同的格式字符串,round
才能检测何时使用它们:
for i in [1002, 1009]:
print(f'{i/1000:.0f}') if round(i/1000, 2) == 1.00 else print(f'{i/1000:.2f}')
产生:
1
1.01
参考Format Specification Mini-Language
The precision is a decimal number indicating how many digits should be displayed after the decimal point for a floating point value formatted with 'f'
and 'F'
, or before and after the decimal point for a floating point value formatted with 'g'
or 'G'
.
取决于您的喜好,是使用我建议的 round
方法还是您提到的 strip
方法。如果您以代码应该清晰、简洁和可维护的方式理解 Pythonic,那么我将对两种格式字符串使用 round
方法,因为它对未来的开发人员非常 readable/understandable。
我想在我的 f 字符串中设置一个精度,但我不希望小数部分中的尾随零。
i = 1002
print(f'{i/1000:.2f}') # 1.00 but this must be 1
i = 1009
print(f'{i/1000:.2f}') # 1.01, this is correct
第一个 print
必须是 1
,第二个 print
是我预期的行为 1.01
。
我尝试了 :g
但它适用于第一个 print
但失败了第二个。
i = 1000
print(f'{i/1000:.2g}') # 1, this is correct but
i = 1009
print(f'{i/1000:.2g}') # 1, but this must be 1.01
我尝试过的一种方法是f'{i/1000:.2f}'.strip('.0')
,但我想知道是否有更好的方法。
编辑:
在我的实际代码中,如果 i
是 100000
,那么分母也将是 100000(i
顺序中的最小数字),换句话说,我的代码中的分母将是始终使 i// 分母始终产生一个数字。
如果你的字符串只有浮点数,那么你可以使用你现在拥有的str.rstrip()
(instead of str.strip()
)。此外,您需要首先使用 '0'
对其进行链式调用,然后使用 '.'
(例如 .rstrip('0').rstrip('.')
)来处理带有尾随零的整数,例如 10000
.
但是,如果您的字符串中可以有其他字符,并且您只想去除数字 0
,那么您可以使用嵌套的 f-string 作为:
>>> f"{f'{1002/1000:.2f}'.rstrip('0').rstrip('.')} number"
'1 number'
>>> f"{f'{1009/1000:.2f}'.rstrip('0').rstrip('.')} number"
'1.01 number'
>>> f"{f'{1000:.2f}'.rstrip('0').rstrip('.')} number"
'1000 number'
您必须使用两种不同的格式字符串,round
才能检测何时使用它们:
for i in [1002, 1009]:
print(f'{i/1000:.0f}') if round(i/1000, 2) == 1.00 else print(f'{i/1000:.2f}')
产生:
1
1.01
参考Format Specification Mini-Language
The precision is a decimal number indicating how many digits should be displayed after the decimal point for a floating point value formatted with
'f'
and'F'
, or before and after the decimal point for a floating point value formatted with'g'
or'G'
.
取决于您的喜好,是使用我建议的 round
方法还是您提到的 strip
方法。如果您以代码应该清晰、简洁和可维护的方式理解 Pythonic,那么我将对两种格式字符串使用 round
方法,因为它对未来的开发人员非常 readable/understandable。