简体   繁体   English

在python中控制浮点精度

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM