Click here to Skip to main content
15,897,187 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I used the below code for a math operation

Convert.ToDouble("80.1") * 100000.0

The answer i was supposed to get is 8010000 (Check in calculator). But the asnwer the above line gave me was 8009999.9999999991. Can anyone please tell me why this is happening and what is the correct way to do this math operation?
Posted
Updated 28-Jun-15 18:14pm
v2
Comments
sasanka sekhar panda 29-Jun-15 0:27am    
Please share some more details as this code work fine for me..
CPallini 2-Jul-15 3:52am    
(Check in calculator)
Fantastic!

There is no issue. This is expected as your computer cannot exactly represent all floating point numbers. See here:
http://effbot.org/pyfaq/why-are-floating-point-calculations-so-inaccurate.htm[^]

and here:
http://stackoverflow.com/questions/2100490/floating-point-inaccuracy-examples[^]

and if you wish to go further here:
http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html[^]

There are many other links you can find on this subject including this:
Five Tips for Floating Point Programming[^]

If you poke a little deeper you will find 100000.0 can be represented exactly but 80.1 cannot being 80.099999999999994. The exact result of the multiplication may also not be able to be represented exactly so we get to your situation.

You may also wish to look at the decimal type which has greater precision over a smaller range. This particularly applies to money values.
https://msdn.microsoft.com/en-us/library/364x0z75.aspx[^]
 
Share this answer
 
v6
Comments
Sergey Alexandrovich Kryukov 29-Jun-15 1:44am    
Sure, a 5.
—SA
[no name] 29-Jun-15 1:53am    
Thanks SA. There must be previous answers relating to this - maybe your own, but I could not find an exact match.
you can use math.Round method for getting the expected output
 
Share this answer
 
Thanks for the reply guys. Like mentioned in the link my computer might be having issues representing floating point correctly.

Using Convert.ToDecimal("80.1") * Parse.Decimal("100000.0") gives me the exact result.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900