Decimals and Rounding
The result of an arithmetic expression contains as many decimal places as are found in the operands of the expression.
If the expression contains several operands with varying numbers of decimal places, the result takes on the number of decimal places found in the operand with the most decimal places.
If the result of the expression is assigned to an object with a numeric data type, the value takes on the number of decimal places defined for that object. If the object has fewer rounded decimal places than the expression’s result, the final value is rounded as shown in the following example:
|5/2||Evaluates to 3|
|5/2.0||Evaluates to 2.5|
|1+1.01||Evaluates to 2.01|
|1.01+(5/2)||Evaluates to 3.51|
Rounding occurs only after the entire expression has been evaluated. Therefore, in 1.01+(5/2), the fractional part of 5/2 is kept during the addition of 1.01. Because 1.01 has two decimal places, the result also has two decimal places.