LarsVegas
LarsVegas

Reputation: 6812

Python: How to manage rounding behavior between float and int?

What is the best way to avoid a rounding problem when doing:

>>> a =8.92138 
>>> a
8.92138 
>>> int(a*100000)
892137

Decimal gives me

>>> Decimal(a)
Decimal('8.921379999999999199644662439823150634765625')

Upvotes: 0

Views: 541

Answers (2)

Steven Rumbalski
Steven Rumbalski

Reputation: 45542

int does not round -- it finds the floor (truncates the fractional part).

>>> n = 8.92138
>>> '%.42f' % n   # what n really is
'8.921379999999999199644662439823150634765625'
>>> 100000 * n # result is slightly lower than 892138
892137.9999999999
>>> int(100000 * n) # int takes the floor
892137

Upvotes: 4

falsetru
falsetru

Reputation: 369044

Use the Decimal from the beginning if possible:

>>> a = Decimal('8.92138')
>>> int(a * 100000)
892138

To round use Decimal.quantize

>>> a = 8.92138
>>> Decimal(a) * 100000
Decimal('892137.9999999999199644662440')
>>> (Decimal(a) * 100000).quantize(1)
Decimal('892138')

>>> str(a)
'8.92138'
>>> int(Decimal(str(a)) * 100000)
892138

Upvotes: 2

Related Questions