Subtracting two float digits in .NET

Subtracting two float digits in .NET

I have faced a very strange issue with the .NET which was subtracting 2 Float digits “1.5 – 1.1” the result should be 0.4 but it was 0.39999999999999991 . This happens because floating point numbers can not be precisely represented in binary, such as 1/4 the result will be 0.

To solve this issue all you have to do is using round function in Math class provided by .NET Framework. So if you do this Math.Round(1.5-1.1,1) the result would be as expected 0.4 .Be careful when dealing with float and decimal especially if you are working on financial application.

Share this post

Leave a Reply

Your email address will not be published.