Pregunta

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?

¿Fue útil?

Solución

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

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top