manpagez: man pages & more
info gawk
Home | html | info | man

File: gawk.info,  Node: OFMT,  Next: Printf,  Prev: Output Separators,  Up: Printing

5.4 Controlling Numeric Output with 'print'
===========================================

When printing numeric values with the 'print' statement, 'awk'
internally converts each number to a string of characters and prints
that string.  'awk' uses the 'sprintf()' function to do this conversion
(*note String Functions::).  For now, it suffices to say that the
'sprintf()' function accepts a "format specification" that tells it how
to format numbers (or strings), and that there are a number of different
ways in which numbers can be formatted.  The different format
specifications are discussed more fully in *note Control Letters::.

   The predefined variable 'OFMT' contains the format specification that
'print' uses with 'sprintf()' when it wants to convert a number to a
string for printing.  The default value of 'OFMT' is '"%.6g"'.  The way
'print' prints numbers can be changed by supplying a different format
specification for the value of 'OFMT', as shown in the following
example:

     $ awk 'BEGIN {
     >   OFMT = "%.0f"  # print numbers as integers (rounds)
     >   print 17.23, 17.54 }'
     -| 17 18

According to the POSIX standard, 'awk''s behavior is undefined if 'OFMT'
contains anything but a floating-point conversion specification.  (d.c.)

© manpagez.com 2000-2025
Individual documents may contain additional copyright information.