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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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.
total=$(echo "total + $incr_num" | bc)

Open in new window

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

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Where is  $incr_num coming from?
YZlatAuthor Commented:
Thank you! Worked like a charm
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Shell Scripting

From novice to tech pro — start learning today.