Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I change the decimal separator in the printf command in bash?

Tags:

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?

like image 968
Ferdinando Randisi Avatar asked Oct 11 '12 17:10

Ferdinando Randisi


People also ask

Can I use printf in bash?

The bash printf command is a tool used for creating formatted output. It is a shell built-in, similar to the printf() function in C/C++, Java, PHP, and other programming languages. The command allows you to print formatted text and variables in standard output.

How to print output in bash script?

Typically, when writing bash scripts, we use echo to print to the standard output. echo is a simple command but is limited in its capabilities. To have more control over the formatting of the output, use the printf command.

How do you change a decimal separator to a dot?

Click File > Options. On the Advanced tab, under Editing options, clear the Use system separators check box. Type new separators in the Decimal separator and Thousands separator boxes. Tip: When you want to use the system separators again, select the Use system separators check box.

What is the correct decimal separator?

In the United States, we use the decimal or period (“.”) to represent the difference between whole numbers and partial numbers. We use the comma (“,”) to separate groups of three places on the whole numbers side. This might be different from the way you currently write numbers.


2 Answers

There are several local variables that control the localization of Cygwin (or of any bash shell, for that 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= 

You can see the possible values of the variables by using locale -va. They are all formatted like <language>_<nation>.UTF-8. The UTF-8 part 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" 

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

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 file.

Hope this was helpful!

like image 182
Ferdinando Randisi Avatar answered Oct 29 '22 20:10

Ferdinando Randisi


If you don't want to mess with system configuration, you can respect your locale but make sure your script uses dots for decimals with:

$ printf "%f" 3.5 -bash: printf: 3,5: invalid number 0.000000  $ LANG=C printf "%f" 3.5 3.500000 
like image 42
jesjimher Avatar answered Oct 29 '22 21:10

jesjimher