YZlat
asked on
Handling float or double in calculations
I am having some issues with floating-point variables in ksh scripting.
I am doing some calculations - adding values within the loop. I know the total adds up to one number bu I see a different (smaller) number printed to a screen. So I started echoing all the numbers being added and realized that ksh script rounds of floating point. If a number is greater then 1 but less than 2, for example (1.2), it uses 1. If number is 0.9, it uses 0, etc.
here is an example:
my code:
echo $incr_num
total=$((total + $incr_num))
echo "total so far "$total
output:
1.02
total so far is 1
0.83
total so far is 1
3.03
total so far is 4
0.90
total so far is 4
22.13
total so far is 26
So I get 26, instead of 27.91
I also tried using bc
total=$(echo "total + $incr_num" | bc)
but that gives me an error "syntax error on line 1 stdin"
Can someone help me fix this?
I am doing some calculations - adding values within the loop. I know the total adds up to one number bu I see a different (smaller) number printed to a screen. So I started echoing all the numbers being added and realized that ksh script rounds of floating point. If a number is greater then 1 but less than 2, for example (1.2), it uses 1. If number is 0.9, it uses 0, etc.
here is an example:
my code:
echo $incr_num
total=$((total + $incr_num))
echo "total so far "$total
output:
1.02
total so far is 1
0.83
total so far is 1
3.03
total so far is 4
0.90
total so far is 4
22.13
total so far is 26
So I get 26, instead of 27.91
I also tried using bc
total=$(echo "total + $incr_num" | bc)
but that gives me an error "syntax error on line 1 stdin"
Can someone help me fix this?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Where is $incr_num coming from?
ASKER
Thank you! Worked like a charm
PSi'm afraid shell does not process float, you need perl or python or anything that knows floats.