pint
pint copied to clipboard
Unexpected output for siunitx format when using Decimal nonint_type
Minimal example: (tested with pint-0.21)
import decimal
from pint import UnitRegistry
ureg = UnitRegistry(non_int_type=decimal.Decimal)
q = ureg("8.989e9 newton * meter^2 / coulomb^2")
print(f"{q:Lx}")
Output:
\SI[]{8.989E+9}{\meter\tothe{2.000}\newton\per\coulomb\tothe{2.000}}
Expected output:
\SI[]{8.989E+9}{\meter\squared\newton\per\coulomb\squared}
For reference, the output when running the example without the non_int_type=decimal.Decimal
argument:
\SI[]{8989000000.0}{\meter\squared\newton\per\coulomb\squared}
This can be fixed by improving the siunitx_format_unit
, which can be found here:
https://github.com/hgrecco/pint/blob/0b69ad47b2069e29ba48eb64b30093d53da163d6/pint/formatting.py#L444
I'm happy to create PR after the ongoing refactoring.
Some ideas for how to handle this:
- Test if a Decimal instances is integer:
d.as_integer_ratio()[1] == 1
- It would be good to fix the analogous bug with the
Fraction
class while we're at it.
This issue probably also belongs to #1757