gawk floating-point number localization

百般思念 提交于 2019-11-28 05:46:08

问题


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?



来源:https://stackoverflow.com/questions/16832395/gawk-floating-point-number-localization

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!