How to get the right current locale decimal mark?

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Post Reply
Message
Author
User avatar
Argolance
Posts: 3767
Joined: Sun 06 Jan 2008, 22:57
Location: PORT-BRILLET (Mayenne - France)
Contact:

How to get the right current locale decimal mark?

#1 Post by Argolance »

Hello,
I need to get the decimal mark currently used by the system for a script to run properly according to the locale settings. French, German, Spanish... are using comma, English, Japan... point. The problem is that, while setting my locale as French for instance, I get the wrong mark using these simple code lines to get it:
# a=$(echo "scale=2;100/3"|bc)
# echo $a
33.33
#
:shock:

I already tried others ways to solve the problem like getting the current locale and, while listing all locales supposed to use point, conditionally run the script with, and with comma if not listed... Of course, this doesn't match every time and I thought it should be easier to get the current mark directly.
How could I solve this? As far as I know, this problem has, at least, to be solved for all scripts using gtk hscale or vscale!

Best regards.

User avatar
L18L
Posts: 3479
Joined: Sat 19 Jun 2010, 18:56
Location: www.eussenheim.de/

Re: How to get the right current decimal mark?

#2 Post by L18L »

# grep -A4 ^LC_NUMERIC /usr/share/i18n/locales/${LANG%.*}
LC_NUMERIC
decimal_point "<U002C>"
thousands_sep "<U002E>"
grouping 3;3
END LC_NUMERIC
#

2C is ,
2E is .
:)

Or just:
grep ^decimal_point /usr/share/i18n/locales/${LANG%.*}

User avatar
Karl Godt
Posts: 4199
Joined: Sun 20 Jun 2010, 13:52
Location: Kiel,Germany

#3 Post by Karl Godt »

I would probably simply use

Code: Select all

echo $LANG
LANG=C dc 1000 3 \/ p
echo $LANG
This should set the locale just for the line/command , not for the whole script .

User avatar
Argolance
Posts: 3767
Joined: Sun 06 Jan 2008, 22:57
Location: PORT-BRILLET (Mayenne - France)
Contact:

#4 Post by Argolance »

Hello,
Thank you Karl Godt and L18L for replying.
@L18L
I think what you suggested is a good way to get the decimal mark and contribute to find a solution to a problem that concerns all scripts using non monetary decimal values (Please see=> JMW theme configuration)
A bit more complicated though, because all locales files don't give the decimal_point value directly but through another locale config file that is "copied" instead:
LC_NUMERIC
copy "de_BE"
[...]
... for instance.
I add code lines to this hscale script based on the original (from gtkdialog(4) svn repository by thunor):
#!/bin/sh

GTKDIALOG=gtkdialog
#get current Decimal Mark
DM_tmp=$(grep ^decimal_point /usr/share/i18n/locales/${LANG%.*} | cut -d'<' -f2 | sed 's/>"//')
if [[ -z $DM_tmp ]]; then
echo $(grep -A4 ^LC_NUMERIC /usr/share/i18n/locales/${LANG%.*}) > /tmp/LC_N_temp
locale=$(cat /tmp/LC_N_temp | cut -d '"' -f2)
DM_tmp=$(grep ^decimal_point /usr/share/i18n/locales/$locale | cut -d'<' -f2 | sed 's/>"//')
fi

if [[ $DM_tmp = "U002C" ]]; then
DM=","
else
DM="."
fi

export MAIN_DIALOG='
<window title="Decimal Mark Test" resizable="true">
<vbox>
<frame hscale widget>
<hscale space-expand="true" space-fill="true"
width-request="200" height-request="20"
value-pos="0"
range-min="0'$DM'3" range-max="30'$DM'7"
range-step="0'$DM'1" range-value="15'$DM'5">
<variable>HSCALE</variable>
</hscale>
</frame>
<hbox homogeneous="true">
<button ok></button>
</hbox>
</vbox>
<action signal="hide">exit:Exit</action>
</window>
'

$GTKDIALOG --program=MAIN_DIALOG
This should probably be polished up because (no use to say!) I am not an expert in code lines :oops: but it works fine (seems to work?) with any locale and this was the purpose! :D

Best regards.

User avatar
Argolance
Posts: 3767
Joined: Sun 06 Jan 2008, 22:57
Location: PORT-BRILLET (Mayenne - France)
Contact:

#5 Post by Argolance »

Hello Karl Godt,
Karl Godt wrote:I would probably simply use

Code: Select all

echo $LANG
LANG=C dc 1000 3 \/ p
echo $LANG
This should set the locale just for the line/command , not for the whole script .
I was wondering how could this be done taking the script above as example?
Thank you a lot.

Regards

User avatar
L18L
Posts: 3479
Joined: Sat 19 Jun 2010, 18:56
Location: www.eussenheim.de/

#6 Post by L18L »

Maybe this is faster without copy, I don´t know

# pwd
/usr/lib/locale/de_DE
# cat LC_NUMERIC
"$(,0,.,.ISO-8859-1#

# pwd
/usr/lib/locale/en_US
# cat LC_NUMERIC
"$(,0.,.,ISO-8859-1#

Post Reply