1.

Solve : Regarding Decimals in Cshell?

Answer»

Hello...

I am new to unix and I am wondering if in a C-shell script , Are we supposed to use only whole NUMBERS........ for example..if a program needs to calculate the average of some numbers........

@ avg = (($1 +$2 + $3)/3)) is returning a whole number.........How can a decimal be achieved in a CShell........

THANQ for your time.........first of all see here. If you want to do arithmetic, use awk , bc or dc.
eg
Code: [Select]awk 'BEGIN{ print 1+2+3/3}'
or
Code: [Select]echo "$1+$2+$3/3" | bc
The C-shell cannot do floating point (decimal) arithmetic, for example, 1.1 * 2.3. However, you can invoke the bc calculator program from within a shell script to perform decimal arithmetic. Probably the simplest WAY to do that is to define an alias, called for example, "MATH", that performs decimal arithmetic via the bc program on the variables or constants passed to the alias.



Discussion

No Comment Found