gawk 浮点数本地化 [英] gawk floating-point number localization
问题描述
我希望 gawk 使用逗号 ,
作为小数点字符来解析数字.所以我将 LC_NUMERIC
设置为 fr_FR.utf-8
但它不起作用:
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
", $1 + 0) }'
123.00
解决方案是指定选项 --posix
或 export POSIXLY_CORRECT=1
但在这种情况下 GNU awk 扩展不可用,例如 delete
或 gensub
函数:
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
", $1 + 0) }'
123,20
是否可以在不指定的情况下使用 ,
作为小数点的 gawk 解析数字POSIX 选项?
Is it possible to have gawk parsing number with ,
as decimal point without specifying
POSIX option?
推荐答案
您正在寻找的选项是:
--use-lc-numeric
这会强制 gawk 在解析输入数据时使用区域设置的小数点字符.虽然 POSIX 标准要求这个行为,当 --posix 生效时 gawk 会这样做,默认值为遵循传统行为并使用句点作为小数点,即使在句点不是小数点的语言环境中特点.此选项覆盖默认行为,没有--posix 选项的完全严格的严格性.
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.
演示:
$ echo 123,2 | LC_NUMERIC=fr_FR.utf-8 awk --use-lc-numeric '{printf "%.2f
",$1}'
123,20
注意:printf
是语句而不是函数,所以括号不是必需的,我不确定你为什么在这里添加零?
Notes: printf
is statement not a function so the parenthesis are not required and I'm not sure why you are adding zero here?
这篇关于gawk 浮点数本地化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!