Rounding off using BC.

Hello again.

I'm trying to use BC to calculate some numbers in a shell script.

I want to have the numbers rounded off to 1 decimal place.
for example:
initsize=1566720
zipsize=4733

I'm trying to get the ratio between them. the equation is:

(($initsize-$zipsize)/$initsize)*100

so I'm trying to do this:

echo "scale=1; (($initsize-$zipsize)/$initsize)*100" | bc

but the answer is coming out as 90.0 instead of 99.6

it's obviously because the first part in the brackets is being rounded off before getting multiplied by 100.

I tried plsitting it up to work out hte first bit at 3 decimal places, then multiplying it by 100 with 1 deciaml place. but it still showed up with 3 decimal places.

any ideas?

I don't understand why it won't work either. But why don't you let bc calculate the number, then format it as a string using something like sed?

$ echo "(($initsize-$zipsize)/$initsize) * 100 " | bc -l
99.69790390114379084900
$ echo "(($initsize-$zipsize)/$initsize) * 100 " | bc -l | sed -e "s/\(\.[0-9]\).*/\1/g"
99.6

yeah it's gotten to me.
I changed the equation to this (same thing just rearranged) and it works. strange!

(($initsize-$zipsize)*100/$initsize)

I'm not sure why you find it strange...

$ bc
bc 1.03 (Nov 2, 1994)
Copyright (C) 1991, 1992, 1993, 1994 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'.
scale=9
(1566720-4733) / 1566720
.996979039
scale=1
(1566720-4733) / 1566720
.9