test/addcomma.awk
raw| 1 | # addcomma - put commas in numbers |
| 2 | # input: a number per line |
| 3 | # output: the input number followed by |
| 4 | # the number with commas and two decimal places |
| 5 | |
| 6 | { printf("%-12s %20s\n", $0, addcomma($0)) } |
| 7 | |
| 8 | function addcomma(x, num) { |
| 9 | if (x < 0) |
| 10 | return "-" addcomma(-x) |
| 11 | num = sprintf("%.2f", x) # num is dddddd.dd |
| 12 | while (num ~ /[0-9][0-9][0-9][0-9]/) |
| 13 | sub(/[0-9][0-9][0-9][,.]/, ",&", num) |
| 14 | return num |
| 15 | } |