What is the difference between decimal, float and double in .NET?

Technology CommunityCategory: .NET CoreWhat is the difference between decimal, float and double in .NET?
VietMX Staff asked 3 years ago
Problem

When would someone use one of these?

Precision is the main difference.

  • Float – 7 digits (32 bit)
  • Double-15-16 digits (64 bit)
  • Decimal -28-29 significant digits (128 bit)

As for what to use when:

  • For values which are “naturally exact decimals” it’s good to use decimal. This is usually suitable for any concepts invented by humans: financial values are the most obvious example, but there are others too. Consider the score given to divers or ice skaters, for example.
  • For values which are more artefacts of nature which can’t really be measured exactly anyway, float/double are more appropriate. For example, scientific data would usually be represented in this form. Here, the original values won’t be “decimally accurate” to start with, so it’s not important for the expected results to maintain the “decimal accuracy”. Floating binary point types are much faster to work with than decimals.