Next: , Previous: Setting Precision, Up: Arbitrary Precision Floats


15.4.2 Setting the Rounding Mode

The ROUNDMODE variable provides program level control over the rounding mode. The correspondence between ROUNDMODE and the IEEE rounding modes is shown in table-gawk-rounding-modes.

Rounding Mode IEEE Name ROUNDMODE
Round to nearest, ties to even roundTiesToEven "N" or "n"
Round toward plus Infinity roundTowardPositive "U" or "u"
Round toward negative Infinity roundTowardNegative "D" or "d"
Round toward zero roundTowardZero "Z" or "z"
Round to nearest, ties away from zero roundTiesToAway "A" or "a"

Table 15.4: gawk Rounding Modes

ROUNDMODE has the default value "N", which selects the IEEE-754 rounding mode roundTiesToEven. In Table 15.4, "A" is listed to select the IEEE-754 mode roundTiesToAway. This is only available if your version of the MPFR library supports it; otherwise setting ROUNDMODE to this value has no effect. See Rounding Mode, for the meanings of the various rounding modes.

Here is an example of how to change the default rounding behavior of printf's output:

     $ gawk -M -v ROUNDMODE="Z" 'BEGIN { printf("%.2f\n", 1.378) }'
     -| 1.37