1

It appears that in every definition I can find of round half to even includes nearest even integer (e.g., Python's decimal documentation), as if ONLY integers are rounded to. However, if I round decimals smaller than 1, it appears to follow the same principal, only assigning the role of integer to the decimal place that I am rounding to. Example:

>>> THREE_PLACES = decimal.Decimal('0.000')
>>>
>>> decimal.Decimal('.0005').quantize(THREE_PLACES)
>>> Decimal('0.000')
>>>
>>> decimal.Decimal('.0015').quantize(THREE_PLACES)
>>> Decimal('0.002')

In this example, the value of third decimal place seems to play the role of the integer (rounding down to 0 and up to 2). Is this the specified way of handling numbers less than zero (and thus how Python's ROUND_HALF_EVEN is supposed to function), and if so, am I just misunderstanding the meaning of "integer" in this context? Or, is there more to the story, and perhaps this is merely a coincidence?

orokusaki
  • 55,146
  • 59
  • 179
  • 257

1 Answers1

1

Your interpretation is correct. The documentation isn't clear and should probably use the word digit instead of integer. ROUND_HALF_EVEN implies the last digit of the result will be even (when rounding away exactly ....5000).

casevh
  • 11,093
  • 1
  • 24
  • 35
  • Sorry, you say that ROUND_HALF_EVEN implies that the last digit of the result will be even, does it concerns decimals too? I am asking this because if you run `>>> getcontext().prec = 2; print(Decimal(254)/Decimal(100000))` using the decimal module, you get this as result: **0.0025** which ends with the odd 5 digit. Could you explain please more in details what ROUND_HALF_EVEN does in such cases? Thank you for the attention! – tonix Jan 22 '15 at 17:57
  • The last digit is rounded to an even number digit only when the amount rounded way is exactly half the value of the last digit. In your example, the amount rounded away is 4/10 of the last digit so normal rounding occurs. The "EVEN" is only triggered when rounding away exactly "HALF". – casevh Jan 22 '15 at 21:25