Operating System - HP-UX
1833589 Members
3778 Online
110061 Solutions
New Discussion

floating point to standard out

 
SOLVED
Go to solution
Allan Pincus
Frequent Advisor

floating point to standard out

How do I get standard out from a script to report decimals?

For example:

(( p = 10 / 3 ))
echo $p
3

Not:

3.333333333333

I can't seem to find a good reference for formatting.

I don't ever have this problem using Perl, or some other language, just a silly shell script I'm trying to create to compute some usage ratios.

Thanks!
5 REPLIES 5
James R. Ferguson
Acclaimed Contributor
Solution

Re: floating point to standard out

Hi Allan:

One way is with 'bc':

# X=1;Y=8;echo "scale=3\n $X/$Y"|bc

Regards!

...JRF...
Anil C. Sedha
Trusted Contributor

Re: floating point to standard out

There you go.. JRF has just resolved your issue.

Regards,
Anil
If you need to learn, now is the best opportunity
Allan Pincus
Frequent Advisor

Re: floating point to standard out

Thanks!!!!

I've been doing this stuff for almost 8 years, and I still learn new things everyday!!

- Allan
James R. Ferguson
Acclaimed Contributor

Re: floating point to standard out

Hi Allan:

...and don't forget 'awk':

# X=1;Y=8;echo "$X $Y"|awk '{printf "%.4f\n",$1/$2}'

Regards!

...JRF...
John Poff
Honored Contributor

Re: floating point to standard out

Hi,

Here is a way using awk. With a script named divider.awk:

{
result = ($1 / $2)
}
END {
printf("result: %12.6f\n", result);
}


You can then do something like this:

echo "10 3" | awk -f divider.awk
result: 3.333333


It isn't pretty, but it is another way to do it. Plus, you can manipulate the output of your numbers with printf.

JP