Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Variable Substitution - floating point numbers 1

Status
Not open for further replies.

ooids

Technical User
May 2, 2001
21
US
Hi!

I have a very basic script:
#!/bin/ksh

A=0.5
B=0.5

C=$((A * B))
echo $C

It returns 0 - How do i get it to output to 2dp / control the precision of the variables?

Many Thanks
 
use 'bc' for floating point arithmetics vlad
+---------------------------+
|#include<disclaimer.h> |
+---------------------------+
 
Yes, I suggest you use 'bc'.
... or 'dc' if you like more exotic RPN syntax.
 
Thanks..

Would either of you be able to give me an example of using bc on my script? I'm a newbie, not used bc before..
 
You might consider a finer precision for the floating point arirthmetics - say FOUR decimal points:

C=$(echo &quot;scale=4;${A} + ${B}&quot; | bc); vlad
+---------------------------+
|#include<disclaimer.h> |
+---------------------------+
 
Thanks a lot for the help - just what i need...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top