0

I am having the function to truncate the decimal places of the double datatype vaiable which is given below

             public double GetTruncate(double tobeTruncated, int divideFactor)
      {            
        if (divideFactor == 0)
            return Math.Truncate((double)tobeTruncated);
        else
        {
            tobeTruncated = tobeTruncated * divideFactor;
            tobeTruncated = Math.Truncate((double)tobeTruncated);
            tobeTruncated = tobeTruncated / divideFactor;
        }
        return tobeTruncated;


    }

Example : tobeTruncated = 35.66 dividefactor = 100

While executing the above line , i am getting the value for tobeTruncated as 3565.9999999999995,instead of 3566. I dont know the reason.

When i changed the double variable to decimal , i am getting the correct answer. The modified function with correct answer is given below :

           public double GetTruncate(double tobeTruncated, int divideFactor)
    {           

        decimal nTempData = (decimal)tobeTruncated;
        if (divideFactor == 0)
            return (double)Math.Truncate((decimal)nTempData);
        else
        {
            nTempData = nTempData * divideFactor;
            nTempData = Math.Truncate((decimal)nTempData);
            nTempData = nTempData / (decimal)divideFactor;
        }
        return (double)nTempData;

    }

Can anyone explain the reason behind this?

mridul raj
  • 75
  • 1
  • 8
Rajesh Kumar G
  • 1,424
  • 5
  • 18
  • 30

2 Answers2

1

In .NET the decimal datatype represents fixed precision numbers. The double datatype represents floating-point numbers, which are an approximation and should not be used when absolute precision is required.

scwagner
  • 3,975
  • 21
  • 16
1

Floating point numbers can only be approximated with the finite amount of bits your computer uses to store them. The result is what you're seeing.

Read: Is double Multiplication Broken in .NET?

Read: Binary floating point and .NET

And read: Why Do Computers Suck at Math?

Community
  • 1
  • 1
Igby Largeman
  • 16,495
  • 3
  • 60
  • 86