how to convert string to numeric in python
<p>I would like to convert string (%) to float.but my method didnt work well.
the result slightly differ from correct number.
for example,</p>

<pre><code>a=pd.Series(data=["0.1%","0.2%"])

0    0.1%
1    0.2%
dtype: object
</code></pre>

<p>first, I strip "%"</p>

<pre><code>a.str.rstrip("%")

0    0.1
1    0.2
dtype: object
</code></pre>

<p>I tried to convert to numeric, but the result is strange.</p>

<p>I guess this phenomena come from binary digit system...</p>

<pre><code>pd.to_numeric(a.str.rstrip("%"))

0    0.10000000000000000555
1    0.20000000000000001110
dtype: float64
</code></pre>

<p>and of course I couldnt convert % to numeric.</p>

<pre><code>pd.to_numeric(a.str.rstrip("%"))/100

0    0.00100000000000000002
1    0.00200000000000000004
dtype: float64
</code></pre>

<p>I also tried .astype(float) method. but The result was same..</p>

<p>why this phenomena happen ? and how can I avoid this phenomena</p>

<p>As a folow-up to the suggestion by @D-Von, the following python packages can be useful to you: <a href="https://docs.python.org/3/library/decimal.html" rel="nofollow">decimal</a> and <a href="https://docs.python.org/3/library/fractions.html" rel="nofollow">fractions</a></p>

<p>Then you can do some things like:</p>

<pre><code>from fractions import Fraction
from decimal import Decimal
f = Fraction(1, 10)
d = Decimal('0.1')
f = f/100
d = d/100
str(d)
</code></pre>

<p>And all the time you are not working with floats but with rational numbers. See the documentation for more examples.</p>

python pandas numpy floating-point