Python3 如果不需要,请删除小数位
Python3 drop decimal places if not needed
我在 Python 3 中看到很多关于格式化浮点数的答案,其中小数位数有限,例如:
>>> "{:.4f}".format(3.14159265359)
'3.1416'
但是这种格式会保留多余的尾随 0:
>>> "{:.4f}".format(3/4)
'0.7500'
我想要的是以一种很好的方式删除尾随零:
>>> "{:.4f??}".format(3/4)
'0.75'
使用 g
格式似乎更接近于此,但它会将小数点前的数字计算为总字段宽度的一部分,例如:
>>> "{:.4g}".format(3/4)
'0.75'
是完美的,但是:
>>> "{:.4g}".format(3.14159265359)
'3.142'
而不是所需的 3.1416
澄清一下,整数(例如 0
本身)根本不应该有小数。
单独使用 format
是否可行,还是我必须通过对格式化数字进行字符串操作来删除尾随零?
我研究的文档页面(除了搜索网络):https://docs.python.org/3/library/string.html#formatspec
我不确定使用裸字符串格式是否可行,但您可以这样做:
>>> a = 3/ 4
>>> "{:.{a}f}".format(a, a=min(len(str(a).split('.')[-1]), 4))
'0.75'
>>> a = 3.14159265359
>>> "{:.{a}f}".format(a, a=min(len(str(a).split('.')[-1]), 4))
'3.1416'
>>>
或者为什么不 rstrip
:
>>> a = 3 / 4
>>> "{:.4f}".format(a).rstrip("0")
'0.75'
>>> a = 3.14159265359
>>> "{:.4f}".format(a).rstrip("0")
'3.1416'
>>>
Numpy 可以做得更好:
>>> import numpy as np
>>> np.format_float_positional(0.75, 4)
'0.75'
>>> np.format_float_positional(np.pi, 4)
'3.1416'
>>>
发帖后我发现的一种方式是:
print('{:.4f}.format(a).rstrip('.0') or '0')
我仍然想知道是否可以更优雅地实现。
要将浮点数转换为小数点后最多 N 位但不包括尾随 0 的字符串,您可以使用 round()
然后转换为字符串。
>>> str(round(3.14159265359, 4))
'3.1416'
>>> str(round(3/4, 4))
'0.75'
>>> str(round(17, 4))
'17'
您可以使用 round function
来完成这项工作。
round function
需要两个 parameters
。
第一个是您要四舍五入的 floating number
,第二个是您想要的 number of places after decimal
。
a=10/3
print(round(a,4))
这将 return 3.3333
。
a=3/4
print(round(a,4))
这将 return 0.75
。
您要查找的是 round
,对于整数条件,您可以使用 float.is_integer
:
def func(x, digits):
x = round(x, digits)
return int(x) if float.is_integer(x) else x
样本 运行
>>> func(3.14159265359, 4)
3.1416
>>> func(3.14000022, 4)
3.14
>>> func(3.000022, 4)
3
PS: 如果需要,您可以将 return 值转换为字符串类型。
我在 Python 3 中看到很多关于格式化浮点数的答案,其中小数位数有限,例如:
>>> "{:.4f}".format(3.14159265359)
'3.1416'
但是这种格式会保留多余的尾随 0:
>>> "{:.4f}".format(3/4)
'0.7500'
我想要的是以一种很好的方式删除尾随零:
>>> "{:.4f??}".format(3/4)
'0.75'
使用 g
格式似乎更接近于此,但它会将小数点前的数字计算为总字段宽度的一部分,例如:
>>> "{:.4g}".format(3/4)
'0.75'
是完美的,但是:
>>> "{:.4g}".format(3.14159265359)
'3.142'
而不是所需的 3.1416
澄清一下,整数(例如 0
本身)根本不应该有小数。
单独使用 format
是否可行,还是我必须通过对格式化数字进行字符串操作来删除尾随零?
我研究的文档页面(除了搜索网络):https://docs.python.org/3/library/string.html#formatspec
我不确定使用裸字符串格式是否可行,但您可以这样做:
>>> a = 3/ 4
>>> "{:.{a}f}".format(a, a=min(len(str(a).split('.')[-1]), 4))
'0.75'
>>> a = 3.14159265359
>>> "{:.{a}f}".format(a, a=min(len(str(a).split('.')[-1]), 4))
'3.1416'
>>>
或者为什么不 rstrip
:
>>> a = 3 / 4
>>> "{:.4f}".format(a).rstrip("0")
'0.75'
>>> a = 3.14159265359
>>> "{:.4f}".format(a).rstrip("0")
'3.1416'
>>>
Numpy 可以做得更好:
>>> import numpy as np
>>> np.format_float_positional(0.75, 4)
'0.75'
>>> np.format_float_positional(np.pi, 4)
'3.1416'
>>>
发帖后我发现的一种方式是:
print('{:.4f}.format(a).rstrip('.0') or '0')
我仍然想知道是否可以更优雅地实现。
要将浮点数转换为小数点后最多 N 位但不包括尾随 0 的字符串,您可以使用 round()
然后转换为字符串。
>>> str(round(3.14159265359, 4))
'3.1416'
>>> str(round(3/4, 4))
'0.75'
>>> str(round(17, 4))
'17'
您可以使用 round function
来完成这项工作。
round function
需要两个 parameters
。
第一个是您要四舍五入的 floating number
,第二个是您想要的 number of places after decimal
。
a=10/3
print(round(a,4))
这将 return 3.3333
。
a=3/4
print(round(a,4))
这将 return 0.75
。
您要查找的是 round
,对于整数条件,您可以使用 float.is_integer
:
def func(x, digits):
x = round(x, digits)
return int(x) if float.is_integer(x) else x
样本 运行
>>> func(3.14159265359, 4)
3.1416
>>> func(3.14000022, 4)
3.14
>>> func(3.000022, 4)
3
PS: 如果需要,您可以将 return 值转换为字符串类型。