Problems operating with decimal numbers (KornShell)

Juanpe

I'm trying to design a script that given any arbitrary command-line input in euros and cents, calculate the change, using the minimum number of notes or coins. The script works fine with numbers with no decimal point, example:

[icapjser@if3tl0060 ]$ ksh my_script.sh 34212
68 500 euro note/s
1 200 euro note/s
1 10 euro note/s
1 2 euro coin/s

The problems start when i pass a number with a decimal point as a parametre, i'll explain a bit how my code works first so you can have a better understanding of the problem, first i create an array with all the different values of notes and coins (500, 200, etc etc..) and then i iterate over that array checking if the result of the division (total_euros / note_or_coin_value ) is greater or equal than 1. If so i use modulus to get the remainding and save the amount of notes/coins i have used. Here is the code if you didn't understand my "amazing" explaining skills.

CANT=$1
RES=''
vals=(500 200 100 50 20 10 5 2 1 0,50 0,20 0,10 0,05 0,02 0,01)
flag=1
i=0

while [ $flag -eq 1 ]; do

n=$(expr $CANT / ${vals[$i]})

if [ $n -ge 1 ]; then     # <- LINE 22


    if [ ${vals[$i]} -gt 2 ]; then
        RES=$RES' '$n' '${vals[$i]}' euro note/s\n'
    else
        RES=$RES' '$n' '${vals[$i]}' euro coin/s\n'
    fi 

    CANT=$(expr $CANT % ${vals[$i]})
fi

if [ $CANT -eq 0 ]; then
    flag=0
fi

i=$i+1

if [ $i -gt 14 ]; then
    flag=0
fi

done

echo -e $RES 

Now why is it that it works perfectly without decimals points, but when they exist it does this:

[icapjser@if3tl0060 ejercicios]$ ksh my_script.sh 3421,32
expr: non-numeric argument
my_script.sh[22]: [: argument expected
expr: non-numeric argument
my_script.sh[22]: [: argument expected
expr: non-numeric argument
my_script.sh[22]: [: argument expected
expr: non-numeric argument
my_script.sh[22]: [: argument expected
expr: non-numeric argument
my_script.sh[22]: [: argument expected
expr: non-numeric argument
my_script.sh[22]: [: argument expected
expr: non-numeric argument
my_script.sh[22]: [: argument expected
expr: non-numeric argument
my_script.sh[22]: [: argument expected
expr: non-numeric argument
my_script.sh[22]: [: argument expected
expr: non-numeric argument
my_script.sh[22]: [: argument expected

I hope you understood the problem, thanks in advanced. PS:If there is something thats not clear just comment and i will update! :P

glenn jackman

You will need to set LC_NUMERIC if you want to use a comma:

$ echo $LC_NUMERIC

$ echo $(( 1.5 * 2.5 ))
3.75
$ export LC_NUMERIC=it_IT
$ echo $(( 1.5 * 2.5 ))  
ksh:  1.5 * 2.5 : arithmetic syntax error
$ echo $(( 1,5 * 2,5 ))
3,75
$ ksh --version
  version         sh (AT&T Research) 93u+ 2012-08-01

and expr cannot do floating point math in any case

$ expr 1.5 \* 2.5
expr: non-integer argument
$ expr 1,5 \* 2,5
expr: non-integer argument

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related