R uses the round half by default, and even in the round() function. But this is not always true when rounding to a certain number of decimal places:
# R code round(1.225,2) #[1] 1.23 round(1.2225,3) #[1] 1.222 round(1.22225,4) #[1] 1.2223 round(1.222225,5) #[1] 1.22222
Comparison with python using decimal module:
# Python code import decimal a = decimal.Decimal("1.225") b = decimal.Decimal("1.2225") c = decimal.Decimal("1.22225") d = decimal.Decimal("1.222225") a.quantize(decimal.Decimal('1.00'), decimal.ROUND_HALF_EVEN)
From python decimal library docs, quantize :
Returns a value equal to the first operand after rounding and having the exponent of the second operand.
I'm not sure I'm right, but it looks like the python result is correct.
Question:
Which one is correct, and how to achieve the correct results using two languages?
source share