How to define and use percentage in Pint
<p>I'm currently using <a href="https://pint.readthedocs.io/en/0.7.2/" rel="nofollow">Pint</a> to handle units and unit conversions. This seems to work well for the units that are already defined in Pint, for example</p>

<pre><code>&gt;&gt;&gt; import pint
&gt;&gt;&gt; ureg = pint.UnitRegistry()
&gt;&gt;&gt; Q = ureg.Quantity
&gt;&gt;&gt; a = Q(5, 'm/s')
&gt;&gt;&gt; a
&lt;Quantity(5, 'meter / second')&gt;
&gt;&gt;&gt; a.to('ft/s')
&lt;Quantity(16.404199475065617, 'foot / second')&gt;
</code></pre>

<p>I tried to <a href="https://pint.readthedocs.io/en/0.7.2/defining.html#programmatically" rel="nofollow">define my own units</a>, which represent percentage. As far as unit conversions go, a percentage is simply 100 times a dimensionless fraction, which is how I defined it.</p>

<pre><code>&gt;&gt;&gt; ureg.define('percent = dimensionless * 100 = pct')
&gt;&gt;&gt; a = Q(5, 'pct')
&gt;&gt;&gt; a
&lt;Quantity(5, 'percent')&gt;
</code></pre>

<p>However I cannot seem to convert back and forth between fraction (<code>'dimensionless'</code>) and <code>'pct'</code>.</p>

<pre><code>&gt;&gt;&gt; a.to('dimensionless')
Traceback (most recent call last):
  File "&lt;pyshell#31&gt;", line 1, in &lt;module&gt;
    a.to('dimensionless')
  File "C:\Python35\python-3.5.1.amd64\lib\site-packages\pint\quantity.py", line 263, in to
    magnitude = self._convert_magnitude_not_inplace(other, *contexts, **ctx_kwargs)
  File "C:\Python35\python-3.5.1.amd64\lib\site-packages\pint\quantity.py", line 231, in _convert_magnitude_not_inplace
    return self._REGISTRY.convert(self._magnitude, self._units, other)
  File "C:\Python35\python-3.5.1.amd64\lib\site-packages\pint\unit.py", line 1026, in convert
    return self._convert(value, src, dst, inplace)
  File "C:\Python35\python-3.5.1.amd64\lib\site-packages\pint\unit.py", line 1042, in _convert
    src_dim = self._get_dimensionality(src)
  File "C:\Python35\python-3.5.1.amd64\lib\site-packages\pint\unit.py", line 813, in _get_dimensionality
    self._get_dimensionality_recurse(input_units, 1.0, accumulator)
  File "C:\Python35\python-3.5.1.amd64\lib\site-packages\pint\unit.py", line 837, in _get_dimensionality_recurse
    self._get_dimensionality_recurse(reg.reference, exp2, accumulator)
  File "C:\Python35\python-3.5.1.amd64\lib\site-packages\pint\unit.py", line 835, in _get_dimensionality_recurse
    reg = self._units[self.get_name(key)]
KeyError: ''
</code></pre>

<p>What I'd essentially like to do is be able to convert between e.g. "0.73" and "73%". How can I define and use such a unit?</p>

<p>It seems that GitHub issue <a href="https://github.com/hgrecco/pint/issues/185" rel="nofollow">hgrecco/pint#185</a> covers the case you're describing.</p>

<p>Using the work-around discussed in that issue works for me using <code>Pint-0.7.2</code>:</p>

<pre><code>from pint.unit import ScaleConverter
from pint.unit import UnitDefinition
import pint

ureg = pint.UnitRegistry()
Q = ureg.Quantity

ureg.define(UnitDefinition('percent', 'pct', (), ScaleConverter(1 / 100.0)))
a = Q(5, 'pct')

print a
print a.to('dimensionless')
</code></pre>

<p>Output:</p>

<pre><code>5 percent
0.05 dimensionless
</code></pre>

python units-of-measurement pint