Skip to main content
added 305 characters in body
Source Link
Stéphane Chazelas
  • 584.9k
  • 96
  • 1.1k
  • 1.7k
#! /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, 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).
  • Floating point numbers are printed as if using printf("%.6g") and integers as if using printf("%d"). Internally, gawk uses your C compiler double and long types internally, but with recent versions can be told to use arbitrary precision with the -M option and the PREC variable.
$ locale decimal_point
.
$ that-script 1.23 -1e2 0x30 010 garbage
1.23 - 100 + 48 + 8 + 0 = -42.77
#! /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).

$ locale decimal_point
.
$ that-script 1.23 -1e2 0x30 010 garbage
1.23 - 100 + 48 + 8 + 0 = -42.77
#! /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
  }
}
  • That 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).
  • Floating point numbers are printed as if using printf("%.6g") and integers as if using printf("%d"). Internally, gawk uses your C compiler double and long types internally, but with recent versions can be told to use arbitrary precision with the -M option and the PREC variable.
$ locale decimal_point
.
$ that-script 1.23 -1e2 0x30 010 garbage
1.23 - 100 + 48 + 8 + 0 = -42.77
Source Link
Stéphane Chazelas
  • 584.9k
  • 96
  • 1.1k
  • 1.7k

#! /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).

$ locale decimal_point
.
$ that-script 1.23 -1e2 0x30 010 garbage
1.23 - 100 + 48 + 8 + 0 = -42.77