gawk floating-point number localization

2019-01-26 19:35发布

问题:

I want gawk to parse number using comma , as the decimal point character. So I set LC_NUMERIC to fr_FR.utf-8 but it does not work:

echo 123,2 | LC_NUMERIC=fr_FR.utf-8 gawk '{printf ("%.2f\n", $1 + 0) }'
123.00

The solution is to specify option --posix or export POSIXLY_CORRECT=1 but in this case the GNU awk extensions are not available, for example delete or the gensub function:

echo 123,2 | LC_NUMERIC=fr_FR.utf-8 gawk --posix '{printf ("%.2f\n", $1 + 0) }'
123,20

Is it possible to have gawk parsing number with , as decimal point without specifying POSIX option?

回答1:

The option your are looking for is:

--use-lc-numeric

This forces gawk to use the locale's decimal point character when parsing input data. Although the POSIX standard requires this behavior, and gawk does so when --posix is in effect, the default is to follow traditional behavior and use a period as the decimal point, even in locales where the period is not the decimal point character. This option overrides the default behavior, without the full draconian strictness of the --posix option.

Demo:

$ echo 123,2 | LC_NUMERIC=fr_FR.utf-8 awk --use-lc-numeric '{printf "%.2f\n",$1}'
123,20

Notes: printf is statement not a function so the parenthesis are not required and I'm not sure why you are adding zero here?