2

I want to use decimal values in shell script but it's not working. I am not getting decimal value. There are few posts which are suggesting to use 'bc' utility of linux. But on my machine 'bc' is not installed and i have no access to install any utility. Now is it possible to handle decimal values without bc . Demo code-:

calculatePercentage()
   {
    Value1=$1
    Value2=$2
    val=`expr $Value1 / $Value2`
    echo $val
    }

h=`calculatePercentage "150" "200"`
echo $h

Output -:

1
abhinav dixit
  • 241
  • 3
  • 14

2 Answers2

5

Below is Program for your convenience

echo $1 $2 | awk '{div=$1/$2; printf"%0.2f\n", div }'

Output :- When executed bash prog.sh 40 23

1.74

Simple one liner program

Vasanta Koli
  • 401
  • 3
  • 6
0

Stretching absurdity:

n1=9381
n2=99999
n1pad="$n1"$(printf "%010d" 0)
result=$(expr $n1pad / $n2)
result=$(printf "%010d" $result | sed 's/\(.\{1,10\}\)$/.\1/')
result=$(printf "%f" $result)
perreal
  • 94,503
  • 21
  • 155
  • 181