[英]Control floating point precision in python
I have a number in python like 2.75960192084e-05. 我在python中有一个数字,例如2.75960192084e-05。 I want to display it with less number of numbers after decimal point like 2.759e-05.
我想用2.759e-05这样的小数点后的数字显示较少的数字。 Note the exponent should remain displayed.
请注意指数应保持显示。 Is that possible ?
那可能吗 ?
the Format Specification Mini-Language allows you do do that with: 格式规范迷你语言允许您使用以下方法做到这一点:
f = 2.75960192084e-05
print('{:9.3e}'.format(f)) # 2.760e-05
...it will round though. ...它会四舍五入。
the 9
means that 9 is the minimal witdh of the resulting string; 9
表示9是结果字符串的最小witdh; with 3
places behind the decimal point in e
xponent notation. 与
3
在小数点后面的地方e
xponent符号。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.