UNIX Programming, Certification, System Administration, Performance Tuning Reference Books
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.

Return to : Unix System Administration Hints and Tips