gpt4 book ai didi

localization - gawk float 本地化

转载 作者:行者123 更新时间:2023-12-04 05:21:27 24 4
gpt4 key购买 nike

我希望 gawk 使用逗号 , 作为小数点字符来解析数字。所以我将 LC_NUMERIC 设置为 fr_FR.utf-8 但它不起作用:

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

解决方案是指定选项 --posixexport POSIXLY_CORRECT=1 但在这种情况下 GNU awk 扩展不可用,例如 deletegensub 函数:

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

是否可以在不指定的情况下将,作为小数点的gawk解析数POSIX 选项?

最佳答案

您正在寻找的选项是:

--use-lc-numeric

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

演示:

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

注意:printf 是语句而不是函数,因此不需要括号,我不确定为什么要在此处添加零?

关于localization - gawk float 本地化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16832395/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com