简体   繁体   English

品脱重要数字,精确度

[英]pint significant figures, precision

I'm looking for a way to tell pint how many significant figures to print. 我正在寻找一种方法来告诉品脱要打印多少重要数据。 For instance, when I type this: 例如,当我输入这个:

import pint
ureg = pint.UnitRegistry()
print(3*ureg.m /9)
0.3333333333333333 meter

You can see that the printed representation has many significant digits. 您可以看到打印的表示有许多有效数字。 Is there a way to set the number of sig digits something like numpy.set_print_options() ... ? 有没有办法设置像numpy.set_print_options()...的sig数字的数量? Or do I have to manually overwrite the Quantity print function? 或者我是否必须手动覆盖数量打印功能?

One option pointed out in gphilo's answer below, I could set the ureg.default_format field in pint, but that doesn't work when trying to print arrays. 在下面的gphilo答案中指出了一个选项,我可以在pint中设置ureg.default_format字段,但是在尝试打印数组时这不起作用。 See here for the error. 请看这里的错误。

Supposing you want 3 significative figures: 假设你想要3个有意义的数字:

print('{:.3f}'.format(3*ureg.m /9))

pint supports PEP 3101 string formatting syntax pint支持PEP 3101字符串格式化语法

Accordint to the version's 0.8.1 documentation you can also set the default formatting via: 根据版本的0.8.1文档,您还可以通过以下方式设置默认格式:

ureg.default_format = '.3f'
print(3*ureg.m /9)

Have a look at: 看一下:

https://pyformat.info/ https://pyformat.info/

""" Python has had awesome string formatters for many years but the documentation on them is far too theoretic and technical. With this site we try to show you the most common use-cases covered by the old and new style string formatting API with practical examples. msgstr“”“Python已经有很多年来很棒的字符串格式化了,但是关于它们的文档太过于理论和技术。通过这个网站,我们尝试向您展示新旧样式字符串格式化API所涵盖的最常见的用例例子。

All examples on this page work out of the box with with Python 2.7, 3.2, 3.3, 3.4, and 3.5 without requiring any additional libraries. 本页面上的所有示例均可与Python 2.7,3.2,3.3,3.4和3.5一起使用,无需任何其他库。

Further details about these two formatting methods can be found in the official Python documentation: """ 有关这两种格式化方法的更多详细信息,请参阅官方Python文档:“”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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