Ads

C# Programming 10 - What is the Difference between float, double and decimal in .NET?

Great to see 😄

Hello friends Myself Chakrapani Upadhyaya a Full Stack Engineer. In this article we will understand what is float, double and decimal also we find out when to use which one.

Why to wait, Lets Start then,..


The main difference between float, double and decimal is just Precision(means size).

  • Float - We can store 7 digits means it holds 32 bit data
  • Double- we can store 15-16 digits means it holds 64 bit data
  • Decimal - we can store 28-29 significant digits means it holds 128 bit data

Now, the main question is when to use what?

As we know decimals are higher precision and its basically used in  financial applications to hold high degree of accuracy values(or to count the values like money).

But problem with this is performance, Decimals are much slower i mean up to 20 times in some scenario than a double/float.

float and double are basically used to measure the values like calculate the distance.

So One more interesting thing here is..

Decimal uses always overflow checking but float and double will not do that

Here is the simple demonstration for that.

decimal myMoney = decimal.MaxValue;

myMoney += 1;

Console.WriteLine(myMoney);

Above code will throw the overflow exception 


But float and double works without any issues

float myDistance = float.MaxValue;

myDistance += 1;

Console.WriteLine(myDistance);

double myDistanceD = double.MaxValue;

myDistanceD += 1;

Console.WriteLine(myDistanceD);

Console.WriteLine("Hello, World!");

And here is the output...


I hope you enjoyed this article. That's it for right now, see you in next article until then take care bye bye 😄

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !