Float, Double, and Decimal in C#

I often meet people struggling with the concept of Float, Double and Decimal in C#, so I wanted to write here a quick and easy comparison between the three types available in C#.

Comparison of Float, Double, Decimal in C# (SO)

Name Digits Bits Precision/Accuracy Speed
Float 7 digits 32 bit lower faster
Double 15-16 digits 64 bit lower faster
Decimal 28-29 significant digits 128 bit higher slower

float flt = 1F/3;
double db = 1D/3;

decimal dcm = 1M/3;

var equal = db == dcm; //Needs a cast
var equal = db == flt; //Does not need a cast

Leave a Reply

Your email address will not be published. Required fields are marked *

5 × = ten