← Back to team overview

openerp-expert-accounting team mailing list archive

Re: [Bug 865387] Re: wrong currency rounding

 

2011/11/6 Ovnicraft <ovnicraft@xxxxxxxxx>:
>>
>> >>> round(2.675, 2)
>> 2.67
>
> The '2'  number is defined in currency.

I perfectly know that. I was doing a python question. If you type
'round(2.675, 2)', python says '2.67', while it should be 2.68.
Citing the page I was referring to:

"The documentation for the built-in round() function says that it
rounds to the nearest value, rounding ties away from zero. Since the
decimal fraction 2.675 is exactly halfway between 2.67 and 2.68, you
might expect the result here to be (a binary approximation to) 2.68.
It’s not, because when the decimal string 2.675 is converted to a
binary floating-point number, it’s again replaced with a binary
approximation, whose exact value is

2.67499999999999982236431605997495353221893310546875

Since this approximation is slightly closer to 2.67 than to 2.68, it’s
rounded down.

If you’re in a situation where you care which way your decimal
halfway-cases are rounded, you should consider using the decimal
module. Incidentally, the decimal module also provides a nice way to
“see” the exact value that’s stored in any particular Python float"


-- 
Lorenzo Battistini


Follow ups

References