gawk: Strings And Numbers
1
1 6.1.4.1 How 'awk' Converts Between Strings and Numbers
1 ......................................................
1
1 Strings are converted to numbers and numbers are converted to strings,
1 if the context of the 'awk' program demands it. For example, if the
1 value of either 'foo' or 'bar' in the expression 'foo + bar' happens to
1 be a string, it is converted to a number before the addition is
1 performed. If numeric values appear in string concatenation, they are
1 converted to strings. Consider the following:
1
1 two = 2; three = 3
1 print (two three) + 4
1
1 This prints the (numeric) value 27. The numeric values of the variables
1 'two' and 'three' are converted to strings and concatenated together.
1 The resulting string is converted back to the number 23, to which 4 is
1 then added.
1
1 If, for some reason, you need to force a number to be converted to a
1 string, concatenate that number with the empty string, '""'. To force a
1 string to be converted to a number, add zero to that string. A string
1 is converted to a number by interpreting any numeric prefix of the
1 string as numerals: '"2.5"' converts to 2.5, '"1e3"' converts to 1,000,
1 and '"25fix"' has a numeric value of 25. Strings that can't be
1 interpreted as valid numbers convert to zero.
1
1 The exact manner in which numbers are converted into strings is
11 controlled by the 'awk' predefined variable 'CONVFMT' (⇒Built-in
Variables). Numbers are converted using the 'sprintf()' function with
1 'CONVFMT' as the format specifier (⇒String Functions).
1
1 'CONVFMT''s default value is '"%.6g"', which creates a value with at
1 most six significant digits. For some applications, you might want to
1 change it to specify more precision. On most modern machines, 17 digits
1 is usually enough to capture a floating-point number's value exactly.(1)
1
1 Strange results can occur if you set 'CONVFMT' to a string that
1 doesn't tell 'sprintf()' how to format floating-point numbers in a
1 useful way. For example, if you forget the '%' in the format, 'awk'
1 converts all numbers to the same constant string.
1
1 As a special case, if a number is an integer, then the result of
1 converting it to a string is _always_ an integer, no matter what the
1 value of 'CONVFMT' may be. Given the following code fragment:
1
1 CONVFMT = "%2.2f"
1 a = 12
1 b = a ""
1
1 'b' has the value '"12"', not '"12.00"'. (d.c.)
1
1 Pre-POSIX 'awk' Used 'OFMT' for String Conversion
1
1 Prior to the POSIX standard, 'awk' used the value of 'OFMT' for
1 converting numbers to strings. 'OFMT' specifies the output format to
1 use when printing numbers with 'print'. 'CONVFMT' was introduced in
1 order to separate the semantics of conversion from the semantics of
1 printing. Both 'CONVFMT' and 'OFMT' have the same default value:
1 '"%.6g"'. In the vast majority of cases, old 'awk' programs do not
1 change their behavior. ⇒Print for more information on the
1 'print' statement.
1
1 ---------- Footnotes ----------
1
1 (1) Pathological cases can require up to 752 digits (!), but we doubt
1 that you need to worry about this.
1