Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unexpected output for siunitx format when using Decimal nonint_type #1772

Closed
tovrstra opened this issue May 7, 2023 · 2 comments · Fixed by #1977
Closed

Unexpected output for siunitx format when using Decimal nonint_type #1772

tovrstra opened this issue May 7, 2023 · 2 comments · Fixed by #1977

Comments

@tovrstra
Copy link
Contributor

tovrstra commented May 7, 2023

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}
@tovrstra tovrstra changed the title Unexpect output for siunitx format when using Decimal nonint_type Unexpected output for siunitx format when using Decimal nonint_type May 7, 2023
@tovrstra
Copy link
Contributor Author

tovrstra commented May 7, 2023

This can be fixed by improving the siunitx_format_unit, which can be found here:

def siunitx_format_unit(units: UnitsContainer, registry) -> str:

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.

@tovrstra
Copy link
Contributor Author

This issue probably also belongs to #1757

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant