gawk floating-point number localization

后端 未结 1 1467
借酒劲吻你
借酒劲吻你 2020-12-19 19:58

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:

相关标签:
1条回答
  • 2020-12-19 20:11

    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?

    0 讨论(0)
提交回复
热议问题