Repository: hgrecco/pint

Issue:
Unit conversion with `Decimal` sometimes introduces trailing zeros
The following example shows somewhat unexpected behavior:

```python
import pint
from decimal import Decimal
ureg = pint.UnitRegistry(non_int_type=Decimal)
a = ureg.Quantity("5.0 mV/m")
b = a.to_base_units()
print(a)
print(b)
print(a.magnitude.as_tuple())
print(b.magnitude.as_tuple())
```

This prints out

```
5.0 millivolt / meter
0.005000 kilogram * meter / ampere / second ** 3.000
DecimalTuple(sign=0, digits=(5, 0), exponent=-1)
DecimalTuple(sign=0, digits=(5, 0, 0, 0), exponent=-6)
```

The conversion to base units, in some cases, introduces extra trailing zeros.
This can be confusing as it gives the impression there are suddenly more significant digits.
When using `pint` in an educational setting, this is not ideal.
(The issue cannot be reproduced with all units, e.g. no problem when repeating this example with `"5.0 mV"`.)

If was able to fix this by changing `1.0` to `1` on the following line:

https://github.com/hgrecco/pint/blob/c07fc9b218ea39965d8d4492e52168fe8f8a644b/pint/util.py#L568

I'm not sure if that could break other things?

(Note that this is just a minor snag. So far `pint` has been super useful! Thanks!!)

Discussion:

Patch:
diff --git a/CHANGES b/CHANGES
index d0a4b1d..e1084b4 100644
--- a/CHANGES
+++ b/CHANGES
@@ -15,11 +15,14 @@ Pint Changelog
   (PR #1663)
 - Changed frequency to angular frequency in the docs.
   (PR #1668)
+- Avoid addition of spurious trailing zeros when converting units and non-int-type is
+  Decimal (PR #1625).
 - Implementation for numpy.delete added for Quantity.
   (PR #1669)
 - Fixed Quantity type returned from `__dask_postcompute__`.
   (PR #1722)
 
+
 ### Breaking Changes
 
 - Support percent and ppm units. Support the `%` symbol.
@@ -27,6 +30,7 @@ Pint Changelog
 - Fix error when parsing subtraction operator followed by white space.
   (PR #1701)
 
+
 0.20.1 (2022-10-27)
 -------------------
 
@@ -63,6 +67,7 @@ types for pint-pandas compatibility. (#1596)
 - New documentation format and organization!
 - Better support for pandas and dask.
 
+
 0.19.2 (2022-04-23)
 -------------------
 
diff --git a/pint/util.py b/pint/util.py
index 97db5d9..420914f 100644
--- a/pint/util.py
+++ b/pint/util.py
@@ -566,7 +566,7 @@ class ParserHelper(UnitsContainer):
         if non_int_type is float:
             return cls(1, [(input_word, 1)], non_int_type=non_int_type)
         else:
-            ONE = non_int_type("1.0")
+            ONE = non_int_type("1")
             return cls(ONE, [(input_word, ONE)], non_int_type=non_int_type)
 
     @classmethod


Test patch:
diff --git a/pint/testsuite/test_issues.py b/pint/testsuite/test_issues.py
index cf7e39c..0c1155c 100644
--- a/pint/testsuite/test_issues.py
+++ b/pint/testsuite/test_issues.py
@@ -1,4 +1,5 @@
 import copy
+import decimal
 import math
 import pprint
 
@@ -1040,6 +1041,21 @@ def test_backcompat_speed_velocity(func_registry):
     assert get("[speed]") == UnitsContainer({"[length]": 1, "[time]": -1})
 
 
+def test_issue1527():
+    ureg = UnitRegistry(non_int_type=decimal.Decimal)
+    x = ureg.parse_expression("2 microliter milligram/liter")
+    assert x.magnitude.as_tuple()[1] == (2,)
+    assert x.to_compact().as_tuple()[1] == (2,)
+    assert x.to_base_units().as_tuple()[1] == (2,)
+    assert x.to("ng").as_tuple()[1] == (2,)
+
+
+def test_issue1621():
+    ureg = UnitRegistry(non_int_type=decimal.Decimal)
+    digits = ureg.Quantity("5.0 mV/m").to_base_units().magnitude.as_tuple()[1]
+    assert digits == (5, 0)
+
+
 def test_issue1631():
     import pint