Next: , Previous: , Up: Arbitrary Precision Floats   [Contents][Index]

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

Round to nearest, ties to evenroundTiesToEven"N" or "n"
Round toward plus InfinityroundTowardPositive"U" or "u"
Round toward negative InfinityroundTowardNegative"D" or "d"
Round toward zeroroundTowardZero"Z" or "z"
Round to nearest, ties away from zeroroundTiesToAway"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