How do I perform arithmetic on floating point numbers?

With addition, bc returns the decimal count of the greater of the arguments:
# echo "10.1 + 11.30" | bc
21.40

# echo "10.1 + 11.3" | bc
21.4

With multiplication/division, you can specify the decimal places:

# echo "scale=5; 10.1 / 11.3" | bc
0.89380

Of course, within double quotes variables are accepted:

# i=10.1; j=11.3
# echo "scale=3; $i / $j" | bc
0.893

Notice that the above employs computational truncation, rather than rounding. Check your bc implementation for your results.

Have a Unix Problem
Do you have a UNIX Question?

Unix Books :-
UNIX Programming, Certification, System Administration, Performance Tuning Reference Books

Return to : - Unix System Administration Hints and Tips

(c) www.gotothings.com All material on this site is Copyright.
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk.
All product names are trademarks of their respective companies.
The site www.gotothings.com is in no way affiliated with or endorsed by any company listed at this site.
Any unauthorised copying or mirroring is prohibited.