呆子浮点数本地化 [英] gawk floating-point number localization

查看:130
本文介绍了呆子浮点数本地化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用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\n", $1 + 0) }'
123.00

解决方案是指定的选项 - POSIX 出口POSIXLY_CORRECT = 1 但在这种情况下,GNU AWK扩展不可用,例如删除 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\n", $1 + 0) }'
123,20

是否有可能与作为小数点GAWK解析数不指定
POSIX选项?

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

推荐答案

您正在寻找的方法是:

- 用LC-数字

这呆子力解析输入数据时使用的语言环境的小数点字符。尽管POSIX标准要求此
  行为,并gawk的这样做时--posix有效时,默认为
  按照传统的行为,并用句点作为小数
  点,即使在语言环境中,其中的周期是不是小数点
  字符。此选项将覆盖缺省行为,而不
  该选项--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\n",$1}'
123,20

注:<?code>的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?

这篇关于呆子浮点数本地化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆