PyQt - 如何在QLabel中禁用科学计数法
我在处理大数字的时候使用了Decimal模块,但当数值小于0.00001时,它会变成科学计数法。有没有办法关闭这个功能,让所有的小数位都显示出来呢?
round(Decimal(str(value)), 9)
'{0:f}'.format(value)这个方法不行,因为它会显示所有的数字,比如0.0000100000,而我只想看到0.00001。
我想在把0.0000000015四舍五入后显示0.000000002。
我试过
def set_decimals(self, value, decimals):
val = '{0:f}'.format(Decimal(str(value)))
rnd_value = round(Decimal(val), decimals)
return str(rnd_value)
但它还是会转换成科学计数法。
谢谢。
2 个回答
0
使用 '{0:f}'.format(value)
这个方式不会得到你想要的结果,因为 float
类型的默认格式化方式会在固定的大小处截断数据。不过,Decimal
类型就不是这样:
>>> '{0:f}'.format(Decimal(str(0.0000000000000001)))
'0.0000000000000001'
默认情况下,使用的格式是 'g'
或 'G'
,具体取决于上下文。
1
我找不到比这个更好的解决办法:
def regularNotation(value):
"""Sometimes str(decimal) makes scientific notation. This function makes the regular notation."""
v = '{:.14f}'.format(value).rpartition('.') # 14 digits in fractional part
return v[0] + (v[1] + v[2]).rstrip('.0') # strip trailing 0s after decimal point