我要如何改变在bash printf命令小数点分隔符? [英] How do i change the decimal separator in the printf command in bash?

查看:144
本文介绍了我要如何改变在bash printf命令小数点分隔符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Cygwin的意大利本地化,因此我的printf命令使用逗号分隔的花车,也不会明白点分隔的花车

I use the Italian localization of Cygwin, and therefore my printf command uses commas to separate floats, and won't understand dot-separated floats

$ printf "%f" 3.1415
-bash: printf: 3.1415: invalid number
0,000000

$ printf "%f" 3,1415
3,141500

这产生了一些问题,因为基本上一切用点分隔的十进制数字。

This gives rise to several problems because basically everything else uses a dot to separate decimal digits.

如何更改从逗号小数点分隔符来点?

How can I change the decimal separator from comma to dot?

推荐答案

有一些局部变量控制塔的cygwin的本地化(或任何bash shell的,对于此事)。您可以使用区域设置命令看到他们连同他们的价值。您应该看到这样的事情:

There are several local variables tha control the localization of cygwin (or of any bash shell, for the matter). You can see them along with their value using the locale command. You should see something like this:

$ locale
LANG=it_IT.UTF-8
LC_CTYPE="it_IT.UTF-8"
LC_NUMERIC="it_IT.UTF-8"
LC_TIME="it_IT.UTF-8"
LC_COLLATE="it_IT.UTF-8"
LC_MONETARY="it_IT.UTF-8"
LC_MESSAGES="it_IT.UTF-8"
LC_ALL=

您可以通过看到的变量可能值区域-va 。他们都像格式化_.UTF-8。 UTF-8是可选的。
为了切换到简单地设置LC_NUMERIC到美国价值北美浮动分离式的。

You can see the possible values of the variables by using locale -va. Their are all formatted like _.UTF-8. UTF-8 is optional. In order to switch to North American float separation style simply set LC_NUMERIC to its American value.

$ export LC_NUMERIC="en_US.UTF-8"

只需设置变量LC_NUMERIC就好像它是一个普通的变量将无法正常工作,您需要使用export命令。

Simply setting the variable LC_NUMERIC as if it were a regular variable won't work, you need to use the export command.

您可以把这个脚本的头部,或者你可以把它永久的将它添加到你的〜/ .bashrc中在〜/ .bash_profile

You can put this in the header of your scripts, or you can make it permanent by adding it to your ~/.bashrc or your ~/.bash_profile

希望这是有帮助的!

这篇关于我要如何改变在bash printf命令小数点分隔符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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