LoudNPossiblyWrong
LoudNPossiblyWrong

Reputation: 3893

C# precision loss when dividing a double

The function bellow is passed a string "1004233" and prints the following output:
D1 = 1.004233
D2 = 0.00423299999999993
D3 = 4232.99999999993
D4 = 4232

I need D4 to print 4233 and not 4232. How do i stop this precision loss from happening?

public string someFunc(String s){
        string retval = "0";
        try{
            int id = int.Parse(s);
            double d = (double)id / (double)1000000;
            Console.WriteLine("D1 = " + d);
            d = d - Math.Truncate(d);
            Console.WriteLine("D2 = " + d);
            d = d * (double)1000000;
            Console.WriteLine("D3 = " + d);
            retval = "" + Math.Truncate(d);
            Console.WriteLine("D4 = " + retval);
        }catch(Exception ex){}
        return retval;
}

Upvotes: 3

Views: 3936

Answers (2)

Craig Trader
Craig Trader

Reputation: 15679

Use decimal arithmetic instead of floating-point (double). More information to be found:

Upvotes: 3

SLaks
SLaks

Reputation: 887547

This is the standard floating-point question.

Use a decimal instead.
Although decimals also don't have infinite precision, they are implemented in base 10, so they will give you the results you expect.

Upvotes: 12

Related Questions