Translate

Thursday, July 4, 2013

c# float vs double vs decimal

Decimal calculations have a higher precision but are 10 times slower than double calculations. Decimal is used for financial calculations.


Float
Double
Decimal
Set x=1
var x=1f;
var x=1d;
var x=1m;
Storage
32 bits
64 bits
128 bits
Range
-1038 to 1038
-10308 to 10308
-1028 to 1028
Smallest number
10-45
10-324
10-28
Significant figures
8
16
29


using System;

namespace ConsoleApplication3
{
  class Program
  {
    static void Main(string[] args)
    {
      float   f = 1f / 6f;
      double  d = 1d / 6d;
      decimal m = 1M / 6M;

      Console.WriteLine(f);//0.1666667                       
      Console.WriteLine(d);//0.166666666666667              
      Console.WriteLine(m);//0.1666666666666666666666666667                 
    }
  }
}


No comments:

Post a Comment

Comments will appear once they have been approved by the moderator