3

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() ... ? 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. See here for the error.

Ethan Keller
  • 963
  • 3
  • 10
  • 26

2 Answers2

4

Supposing you want 3 significative figures:

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

pint supports PEP 3101 string formatting syntax

Accordint to the version's 0.8.1 documentation you can also set the default formatting via:

ureg.default_format = '.3f'
print(3*ureg.m /9)
GPhilo
  • 18,519
  • 9
  • 63
  • 89
  • 1
    This is good, but I want to globally set my preference so I don't have to write this all the time. I'd like to overwrite the pint print function or somehow set it. – Ethan Keller Sep 22 '17 at 14:55
  • 2
    this is precisely what I am looking for. Thanks for sending it. Now my goal is to set this within my own ureg definition so that it is applied automatically for all of my files automatically when I import my own ureg. It looks like adding it to my unit_registry file works beautifully! – Ethan Keller Sep 22 '17 at 15:23
  • this doesn't handle significant digits correctly. I want something that will always display some arbitrary number of digits and use scientific notations. For example, if I said 2 sigfigs and the number was 12345, I would want 1.2e4 and for 34, I'd want it to leave it alone. – Ethan Keller Sep 22 '17 at 16:09
  • 1
    By setting the ureg default format, I ended up breaking the functionality of printing unit arrays. I updated the answer with the issue with this method. – Ethan Keller Oct 11 '17 at 22:13
  • 1
    Doesn't work for me (pint v0.11). ```ureg.default_format = '.3f~P'``` changes the way units are displayed (```~P```) but not the numeric part (```.3f```). Should I just file an issue? – Ilya Apr 29 '20 at 18:18
  • I'm not sure changing the default format is still supported, I couldn't find a reference to it in the current version's docs. – GPhilo Apr 29 '20 at 18:22
  • It doesn't appear to work now. I'm running into the same issue inside a Juptyer notebook. – geekly Dec 03 '21 at 12:44
0

Have a look at:

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.

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.

Further details about these two formatting methods can be found in the official Python documentation: """

Salva.
  • 99
  • 1
  • 1
  • 8