Operating System - HP-UX
1829020 Members
2346 Online
109986 Solutions
New Discussion

Arithmetic operation on decimals

 
SOLVED
Go to solution
SKSingh_1
Frequent Advisor

Arithmetic operation on decimals

How to do arithmetic operations on decimal.

like adding 142.5 and 586.1?
5 REPLIES 5
Dennis Handly
Acclaimed Contributor

Re: Arithmetic operation on decimals

You are either going to have to use awk, bc(1) or dc(1) or write a C program.

Or use fixed point scaled arithmetic.
I'm not sure if dtksh supports it?
Dennis Handly
Acclaimed Contributor

Re: Arithmetic operation on decimals

James R. Ferguson
Acclaimed Contributor

Re: Arithmetic operation on decimals

Hi:

HP-UX offers a Korn93 shell which supports floating point arithmetic:

# cat ./domath
#!/usr/dt/bin/dtksh
typeset -F R1
typeset -F3 R2
typeset -E R3
typeset -F1 R4
let R1=1/8
echo ${R1}
let R2=1/8
echo ${R2}
let R3=1/8
echo ${R3}
let R4=142.5+586.1
echo ${R4}

# ./domath
.1250000000
.125
0.125
728.6

Regards!

...JRF...
Hein van den Heuvel
Honored Contributor
Solution

Re: Arithmetic operation on decimals

If the question is really...
>> How to do arithmetic operations on decimal.
like adding 142.5 and 586.1?

Then, might I suggest one of those $1.50 calculators you can get at the local supermarkets?


ok, I'm teasing you here, but you see what I mean?
What is the real problem you are trying to solve?
Where are the numbers coming from, where are they going to be consumed?


The shell solution MIGHT be a quick awk program, or 'bc' or 'dc'

For example:

$ TEST=$(echo 142.5 + 586.1 | bc -l)
$ echo $TEST
728.6


Cheers,
Hein.
SKSingh_1
Frequent Advisor

Re: Arithmetic operation on decimals

Yes, got the answer Thank you.