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


total so far is 1

total so far is 1

total so far is 4

total so far is 4

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?
LVL 35
Who is Participating?
TintinConnect With a Mentor Commented:
total=$(echo "total + $incr_num" | bc)

Open in new window

should be
total=$(echo "$total + $incr_num" | bc)

Open in new window

it is not really float... just shift ledt, do integercalculations and shift back right.... no loss of INTEGER precision.

PSi'm afraid shell does not process float, you need perl or python or anything that knows floats.
Where is  $incr_num coming from?
YZlatAuthor Commented:
Thank you! Worked like a charm
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.