DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK
 

(gawk.info) Conversion

Info Catalog (gawk.info) Variables (gawk.info) Expressions (gawk.info) Arithmetic Ops
 
 Conversion of Strings and Numbers
 =================================
 
    Strings are converted to numbers, and numbers to strings, if the
 context of the `awk' program demands it.  For example, if the value of
 either `foo' or `bar' in the expression `foo + bar' happens to be a
 string, it is converted to a number before the addition is performed.
 If numeric values appear in string concatenation, they are converted to
 strings.  Consider this:
 
      two = 2; three = 3
      print (two three) + 4
 
 This prints the (numeric) value 27.  The numeric values of the
 variables `two' and `three' are converted to strings and concatenated
 together, and the resulting string is converted back to the number 23,
 to which four is then added.
 
    If, for some reason, you need to force a number to be converted to a
 string, concatenate the empty string, `""', with that number.  To force
 a string to be converted to a number, add zero to that string.
 
    A string is converted to a number by interpreting any numeric prefix
 of the string as numerals: `"2.5"' converts to 2.5, `"1e3"' converts to
 1000, and `"25fix"' has a numeric value of 25.  Strings that can't be
 interpreted as valid numbers are converted to zero.
 
    The exact manner in which numbers are converted into strings is
 controlled by the `awk' built-in variable `CONVFMT' ( Built-in
 Variables).  Numbers are converted using the `sprintf' function
 ( Built-in Functions for String Manipulation String Functions.)
 with `CONVFMT' as the format specifier.
 
    `CONVFMT''s default value is `"%.6g"', which prints a value with at
 least six significant digits.  For some applications you will want to
 change it to specify more precision.  On most modern machines, you must
 print 17 digits to capture a floating point number's value exactly.
 
    Strange results can happen if you set `CONVFMT' to a string that
 doesn't tell `sprintf' how to format floating point numbers in a useful
 way.  For example, if you forget the `%' in the format, all numbers
 will be converted to the same constant string.
 
    As a special case, if a number is an integer, then the result of
 converting it to a string is _always_ an integer, no matter what the
 value of `CONVFMT' may be.  Given the following code fragment:
 
      CONVFMT = "%2.2f"
      a = 12
      b = a ""
 
 `b' has the value `"12"', not `"12.00"' (d.c.).
 
    Prior to the POSIX standard, `awk' specified that the value of
 `OFMT' was used for converting numbers to strings.  `OFMT' specifies
 the output format to use when printing numbers with `print'.  `CONVFMT'
 was introduced in order to separate the semantics of conversion from
 the semantics of printing.  Both `CONVFMT' and `OFMT' have the same
 default value: `"%.6g"'.  In the vast majority of cases, old `awk'
 programs will not change their behavior.  However, this use of `OFMT'
 is something to keep in mind if you must port your program to other
 implementations of `awk'; we recommend that instead of changing your
 programs, you just port `gawk' itself!   The `print' Statement
 Print, for more information on the `print' statement.
 
Info Catalog (gawk.info) Variables (gawk.info) Expressions (gawk.info) Arithmetic Ops
automatically generated byinfo2html