An expression that evaluates to an integer that specifies the number of places to round to, counting from the decimal point. Can be zero, a positive integer, or a negative integer. If scale is a fractional number, Caché rounds it to the nearest integer.
This function can be used to either round or truncate a number to the specified number of decimal digits.
ROUND rounds or truncates numeric-expr to scale places, counting from the decimal point. When rounding, the number 5 is always rounded up. Trailing zeroes are removed after a ROUND round or truncate operation. Leading zeros are not returned.
If scale is a positive number, rounding is performed at that number of digits to the right of the decimal point. If scale is equal to or larger than the number of decimal digits, no rounding or zero filling occurs.
If scale is zero, rounding is to the closest whole integer. In other words, rounding is performed at zero digits to the right of the decimal point; all decimal digits and the decimal point itself are removed.
If scale is a negative number, rounding is performed at that number of digits to the left of the decimal point. If scale is equal to or larger than the number of integer digits in the rounded result, zero is returned.
If numeric-expr is zero (however expressed: 00.00, -0, etc.) ROUND returns 0 (zero) with no decimal digits, regardless of the scale value.
Note that the ROUND return value is always normalized, removing trailing zeros.
ROUND, TRUNCATE, and $JUSTIFY
ROUND and TRUNCATE are numeric functions that perform similar operations; they both can be used to decrease the number of significant decimal or integer digits of a number. ROUND allows you to specify either rounding (the default), or truncation; TRUNCATE does not perform rounding. ROUND returns the same data type as numeric-expr; TRUNCATE returns numeric-expr as data type NUMERIC, unless numeric-expr is data type DOUBLE, in which case it returns data type DOUBLE.
ROUND rounds (or truncates) to a specified number of fractional digits, but its return value is always normalized, removing trailing zeros. For example, ROUND(10.004,2) returns 10, not 10.00.
TRUNCATE truncates to a specified number of fractional digits. If the truncation results in trailing zeros, these trailing zeros are preserved. However, if scale is larger than the number of fractional decimal digits in the canonical form of numeric-expr, TRUNCATE does not zero-pad.
Use $JUSTIFY when rounding to a fixed number of fractional digits is important for example, when representing monetary amounts. $JUSTIFY returns the specified number of trailing zeros following the rounding operation. When the number of digits to round is larger than the number of fractional digits, $JUSTIFY zero-pads. $JUSTIFY also right-aligns the numbers, so that the DecimalSeparator characters align in a column of numbers. $JUSTIFY does not truncate.
$DOUBLE IEEE floating point numbers are encoded using binary notation. Most decimal fractions cannot be exactly represented in this binary notation. When a $DOUBLE value is input to ROUND with a scale value and the rounding flag (flag=0, the default), the return value frequently contains more fractional digits than specified in scale because the fractional decimal result is not representable in binary, so the return value must be rounded to the nearest representable $DOUBLE value, as shown in the following example:
If you are using ROUND to truncate a $DOUBLE value (flag=1), the return value for the $DOUBLE is truncated to the number of fractional digits specified by scale. The TRUNCATE function also truncates a $DOUBLE to the number of fractional digits specified by scale.
If you are using ROUND to round a $DOUBLE value and wish to return a specific scale, you should convert the $DOUBLE value to decimal representation before rounding the result.