cancel
Showing results for 
Search instead for 
Did you mean: 

bc doesnt do maths

 
SOLVED
Go to solution
Highlighted
Valued Contributor

bc doesnt do maths


(15/31)*100 = 48.387096774
or
(15/31)*100 = 48

how can I do the above maths using shell script ?

bc say/produce '0' when 15 is divided by 31

please help
Regards
7 REPLIES 7
Highlighted
Honored Contributor
Solution

Re: bc doesnt do maths

You can use:

echo "(15/31)*100" | bc -l
Por que hacerlo dificil si es posible hacerlo facil? - Why do it the hard way, when you can do it the easy way?
Highlighted
Valued Contributor

Re: bc doesnt do maths

I believe you can do it like this:
echo "scale=2; (15/31)*100" | bc

the scale=2 is just to tell bc that you want only 2 decimals.

quote from http://www.kingcomputerservices.com/unix_101/using_bc_part_1.htm
"The number of decimals to be printed by bc is controlled by the scale variable. The default scale is 0."

Best regards
Fredrik Eriksson
Highlighted
Acclaimed Contributor

Re: bc doesnt do maths

Hi:

If you want to use 'bc' add the 'scale' factor to define the number of digits to the right of the decimal place:

# echo "scale=9;(15/31)*100"| bc
48.387096700

Other ways to accomplish your task, include 'awk' and 'perl' or a shell line Korn93 that supports real numbers:

# awk 'BEGIN{printf "%2.9f\n",(15/31)*100}'
48.387096774

# perl -e 'printf "%2.9f\n",(15/31)*100'
48.387096774

Regards!

...JRF...
Highlighted
Valued Contributor

Re: bc doesnt do maths

thanks everyone for nice help ;)

please help me in the following code

#!/bin/bash
a=19
b=17

s1=$(echo "($a/31)*100" | bc -l)
s2=$(echo "($b/31)*100" | bc -l)

#s1=$(awk 'BEGIN{printf "%2.0f\n",($a/31)*100}')
#s2=$(awk 'BEGIN{printf "%2.0f\n",($b/31)*100}')


if [ $s1 -gt $s2 ]; then echo "Holiday"
else echo "More work"
fi

Regards
Highlighted
Honored Contributor

Re: bc doesnt do maths

If your error is:

line 12: [: 61.29032258064516129000: integer expression expected

then add scale=0 to bc.

#!/bin/bash
a=19
b=17

s1=$(echo "scale=0;($a/31)*100" | bc -l)
s2=$(echo "scale=0;($b/31)*100" | bc -l)

#s1=$(awk 'BEGIN{printf "%2.0f\n",($a/31)*100}')
#s2=$(awk 'BEGIN{printf "%2.0f\n",($b/31)*100}')


if [ $s1 -gt $s2 ]; then echo "Holiday"
else echo "More work"
fi
Por que hacerlo dificil si es posible hacerlo facil? - Why do it the hard way, when you can do it the easy way?
Highlighted
Valued Contributor

Re: bc doesnt do maths

>If your error is:
>line 12: [: 61.29032258064516129000: integer expression expected

>then add scale=0 to bc.

vi script.sh
#!/bin/bash
.
.
s1=$(echo "scale=0;($a/31)*100" | bc -l)
s2=$(echo "scale=0;($b/31)*100" | bc -l)
.
.

# bash -x script.sh
+ a=19
+ b=17
++ echo 'scale=0;(19/31)*100'
++ bc -l
+ s1=0
++ echo 'scale=0;(17/31)*100'
++ bc -l
+ s2=0
+ '[' 0 -gt 0 ']'
+ echo 'More work'
More work

when using "scale=0", you can check/see that s1 and s2 both equals to 0

Regards
Highlighted
Honored Contributor

Re: bc doesnt do maths

You are right, there may be better ways, but you can do:

echo "(17/31)*100" | bc -l | cut -f 1 -d "."

To remove the decimals.
Por que hacerlo dificil si es posible hacerlo facil? - Why do it the hard way, when you can do it the easy way?