Operating System - HP-UX
1846636 Members
1631 Online
110256 Solutions
New Discussion

BASIC CALCULATION THRU Korn Shell

 
SOLVED
Go to solution

BASIC CALCULATION THRU Korn Shell

Dear All!!

Need your help to perform simple calculation thru korn shell.

e.g #1
A=3
let B=$A*2
6
Rem = I got it and correct
e.g #2
A=3
let B=$A*1.33
3
Rem = The answer is 3 not 3.99,
Please assist me on how could I get 3.99

TQVM

7 REPLIES 7
Darren Prior
Honored Contributor
Solution

Re: BASIC CALCULATION THRU Korn Shell

Hi,

I'm afraid that ksh can only provide integer arithmetic - see man ksh. How about using something like bc or dc? There are man pages with examples of how to use them.

regards,

Darren.
Calm down. It's only ones and zeros...
melvyn burnard
Honored Contributor

Re: BASIC CALCULATION THRU Korn Shell

As you said in hte subject line, this is a BASIC calculation, and ksh can only handle integers.
My house is the bank's, my money the wife's, But my opinions belong to me, not HP!
Tim D Fulford
Honored Contributor

Re: BASIC CALCULATION THRU Korn Shell

try

a=1.33
b=2
ans=$(echo $a $b | awk '{printf"%4.2f\n",$1*$2}')
echo $ans

You can format your output with the printf statement, alternatively just use {print $1*$2} & see waht format you get!

Tim
-
Dagmar Boelen
Frequent Advisor

Re: BASIC CALCULATION THRU Korn Shell

Hi,

You can try awk. awk is capable of handling the calculation you mentioned.
RAC_1
Honored Contributor

Re: BASIC CALCULATION THRU Korn Shell

I do it his way.

echo "1*2.33"|bc
There is no substitute to HARDWORK

Re: BASIC CALCULATION THRU Korn Shell

Thanks A LOT for your help. I really apprecited it.

Have a nice day...
Leif Halvarsson_2
Honored Contributor

Re: BASIC CALCULATION THRU Korn Shell

Hi,
Sorry for this late answere. It is perhaps forgotten that it is possible to do simple "floating point" calculations with integer arithmetic. It was nessecary with old system which have no floating point support at all. Try the following (3* 1.33):

echo $((3*133/100)).$((3*133%100))

% is the "mod" operator, the rest in a integer division.