Search code examples
pythonmathscientific-computing

converting numbers to metric unit in python


when doing this in python 100*0.000001 I got 9.999999999999999e-05 What I need to do to get 1e-05?


Solution

  • floating point numbers are not exact. You could represent it as 1e-4 when printing, or use Decimal to get an exact value. e.g.

    >>> print '{:4.0e}'.format(100*0.000001)
    1e-04
    

    or

    >>> Decimal(100)*Decimal('0.000001')
    Decimal('0.000100')