Subtracting 2 Numbers Problem

I am trying to subtract 2 numbers; the numbers come from different sources (one from the Internet, the other from a table in a database where its type is decimal.

If they both have the same value, when I try to subtract them, I get a result like 7.105427357601e-015. I tried casting to int (.to_i) and it works fine but I lose the decimal part.  If I cast them to float I get a similar result to that given above.  I cannoy think of any other cast to make.

Another thing if the two numbers are 84.21 and 80.21, the difference is 3.9999999999999

Subtracting 84.21 from 80.18 produces the correct value.

Sometimes I despair.

Can anyone help?

witgrefeAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

Gertone (Geert Bormans)Information ArchitectCommented:
7.105427357601e-015 is a very, very, very small number,
so this is a precision issue

I assume that you don't want to loose the decimal part,
so you must have some precision in mind.

What you could do is multiply by 100, then round and the divide by 100 again.
Then you will have a precison up to 0.01

3.9999999999999 will become 4.00
and 7.105427357601e-015  will become 0

If you need more precision, multiply by thousand, round and divide again
0
Gertone (Geert Bormans)Information ArchitectCommented:
Try this

result = 3.99999999999
puts (result * 100).round.div(100).to_s

and

result = 7.105427357601e-015
puts (result * 100).round.div(100).to_s
0
Gertone (Geert Bormans)Information ArchitectCommented:
I ran into two traps here
div gives an integer result... that is not what you want
/ would return a truncated integer since the first argument is an integer
so, I need explicit casting to float first

puts ((result * 100).round.to_f / 100).to_s
0
witgrefeAuthor Commented:
But I need 2 decimal places, what you have suggested gives me no decimal places.  It is fine if for the 2 examples I gave, but if I subtract 80.55 from 85.7, I get  5 without any decimal place
0
Gertone (Geert Bormans)Information ArchitectCommented:
Yep, I spotted the error
please look at my last comment
0

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
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
Ruby

From novice to tech pro — start learning today.