26

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?

Quasímodo
  • 3,812
  • 14
  • 25
Ferdinando Randisi
  • 4,068
  • 6
  • 32
  • 43

3 Answers3

29

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!

Gabriel Staples
  • 36,492
  • 15
  • 194
  • 265
Ferdinando Randisi
  • 4,068
  • 6
  • 32
  • 43
  • 1
    You could just set `LC_NUMERIC="en_US.UTF-8"`, but that does not change the setting in the whole system, but in the scope of the script or the shell the setting is done in. Besides, you could change the locale just for the command by `LC_NUMERIC="en_US.UTF-8" printf "%f" 3.1415`. – jarno Sep 08 '15 at 22:41
  • 1
    See answer below for a better (IMO) solution that works independently of locales that you have installed. – mav Apr 08 '16 at 08:33
  • For more on what `export` is, and when and how to use it, see my answer here: https://stackoverflow.com/a/62626515/4561887. – Gabriel Staples Dec 27 '20 at 20:59
12

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
jesjimher
  • 1,175
  • 1
  • 12
  • 17
3

Use:

$ VAR=3,1415
$ echo ${VAR/,/.}

3.1415

or

$ VAR=${VAR/,/.}
$ echo $VAR

3.1415

Another example:

VAR=$(echo "scale=1; $NUMBER/60" | bc)
VAR=${VAR/./,}

$ VAR=$(echo "scale=1; 150/60" | bc);echo $VAR

2.5

$ VAR=$(echo "scale=1; 150/60" | bc); VAR=${VAR/./,};echo $VAR

2,5
  • Welcome to Stack Overflow! While this code may solve the question, [including an explanation](//meta.stackexchange.com/q/114762) of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please [edit] your answer to add explanations and give an indication of what limitations and assumptions apply. –  Jun 24 '20 at 10:17