pythoniku
pythoniku

Reputation: 3672

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?

Upvotes: 1

Views: 628

Answers (2)

dawg
dawg

Reputation: 104072

Don't do the multiplication:

>>> 100e-7
1e-05

Realize though that 0.1 is an infinitely repeating number in binary and you will discover approximations other artifacts before too long:

>>> 100e-7*.1
1.0000000000000002e-06

Then just deal with the issue in formating the output:

>>> '{:e}'.format(100*0.000001)
'1.000000e-04'
>>> '{:e}'.format(100*0.0000001)
'1.000000e-05'
>>> '{:e}'.format(100*0.00000001)
'1.000000e-06'

Upvotes: 0

mgilson
mgilson

Reputation: 310097

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')

Upvotes: 5

Related Questions