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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I create numeric variables with decimals and divide these? 1

Status
Not open for further replies.
Dec 12, 2007
19
US
How do I put the first two numbers in numeric variables and divide the first by the second to get the third number displayed below in a KORN shell script?

2.322359
5.864941
0.395973

( 2.322359 / 5.864941 ) = 0.395973

Thanks,

Mark
 
Code:
echo "scale=6; 2.322359 / 5.864941" | bc -l

"[tt]man bc[/tt]" for more info.


 
Or, using numeric variables...
Code:
FIRST=2.322359
SECOND=5.864941

RESULT=$(echo "scale=6; ${FIRST} / ${SECOND}" | bc -l)

print "Result is ${RESULT}"

Or something like that.

These aren't really "numeric variables" though, just string variables that are holding the numbers to be used in the calculation. The Korn shell has an Integer variable type, but not a floating point variable type (look for "typeset" in the man page).


 
Sam's code worked great. Someone also told me about changing ksh to ksh93 and the simple divide I had in my code came up with the correct answer too.

Thanks Sam
 
In case you use bash.. In
.bashrc put
Code:
function  ca () { echo "$*" | bc -l; }

So when you need to do some simple math operation

Code:
$ca 2.322359 / 5.864941
.39597312232126461289
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top