#! /usr/bin/gawk -E
BEGIN {
sum = 0
if (ARGC > 1) {
for (i = 1; i < ARGC; i++) {
n = strtonum(ARGV[i])
printf "%s", i == 1 ? n : n < 0 ? " - " (-n) : " + "n
sep = " + "
sum += n
}
print " = "sum
}
}
Which should accept floating point (but not inf, nan or things like 0x1p6), hexadecimal and octal numbers. For each argument, only the leading part (ignoring blanks) that can be interpreted as a number is considered. Note that the locale is honoured for the decimal-point (. in English, , in many other languages).
- That should accept floating point (but not
inf,nanor things like0x1p6), hexadecimal and octal numbers - For each argument, only the leading part (ignoring blanks) that can be interpreted as a number is considered.
- Note that the locale is honoured for the decimal-point (
.in English,,in many other languages). - Floating point numbers are printed as if using
printf("%.6g")and integers as if usingprintf("%d"). Internally,gawkuses your C compilerdoubleandlongtypes internally, but with recent versions can be told to use arbitrary precision with the-Moption and thePRECvariable.
$ locale decimal_point
.
$ that-script 1.23 -1e2 0x30 010 garbage
1.23 - 100 + 48 + 8 + 0 = -42.77