Issues when dividing

Hi,
I do have a very simple task to divide 2 variables and display the result.
I CANNOT use bc
when i try

var1=2
var2=4
var3=$(($var1 / $var2))
echo $var3  

the output is always 0
What can I change to get a dotted decimal result such as 0.5 ?
Thanks!

How about using awk:

var1=2
var2=4
var3=$(awk "BEGIN { print $var1 / $var2 }" /dev/null)
echo $var3
1 Like

Thanks a lot. That works!

Just curious. Why can't you use bc?

Hi,
you can achieve that using bc as follows

$ echo " scale=3;2/4"|bc -l
.500

thanks,
venkat

If you are using ksh93 (or another shell that supports typeset), declare variables to be floats and save the overhead of calling awk:

#!/usr/dt/bin/dtksh

# Declare variables as floats.
typeset -F var1=2
typeset -F var2=4

# Declare answer to hold a float with 1 decimal point.
typeset -F1 var3=$(( $var1 / $var2 ))

echo $var3

exit 0

Output:

$ float_test
.5
$