What is the difference between a float, double and a decimal in C#?

Float, double, and decimal are all value types in C# that represent numeric data with fractional parts. Each type differs in precision, memory size, and intended use cases. Understanding these differences is crucial for choosing the appropriate type for your specific needs.

Syntax

Following is the syntax for declaring float, double, and decimal variables −

float floatValue = 3.14f;      // 'f' suffix required
double doubleValue = 3.14;     // default for literals
decimal decimalValue = 3.14m;  // 'm' suffix required

Float Value Type

Float is a 32-bit single-precision floating-point type with a range of approximately ±3.4 × 1038 and provides about 7 decimal digits of precision. It uses 4 bytes of memory and requires the f suffix for literals.

Example

using System;

class Program {
   public static void Main() {
      float a = 3.14159f;
      float b = 2.5f;
      float result = a * b;
      
      Console.WriteLine("Float a: " + a);
      Console.WriteLine("Float b: " + b);
      Console.WriteLine("Result: " + result);
      Console.WriteLine("Type: " + a.GetType());
   }
}

The output of the above code is −

Float a: 3.141589
Float b: 2.5
Result: 7.853975
Type: System.Single

Double Value Type

Double is a 64-bit double-precision floating-point type with a range of approximately ±5.0 × 10-324 to ±1.7 × 10308 and provides about 15-17 decimal digits of precision. It uses 8 bytes of memory and is the default type for floating-point literals.

Example

using System;

class Program {
   public static void Main() {
      double a = 3.141592653589793;
      double b = 2.5;
      double result = a * b;
      
      Console.WriteLine("Double a: " + a);
      Console.WriteLine("Double b: " + b);
      Console.WriteLine("Result: " + result);
      Console.WriteLine("Type: " + a.GetType());
   }
}

The output of the above code is −

Double a: 3.141592653589793
Double b: 2.5
Result: 7.853981633974482
Type: System.Double

Decimal Value Type

Decimal is a 128-bit precise decimal type with a range of approximately ±7.9 × 1028 and provides 28-29 significant digits of precision. It uses 16 bytes of memory and requires the m suffix for literals. It's designed for financial and monetary calculations.

Example

using System;

class Program {
   public static void Main() {
      decimal a = 3.141592653589793238462643m;
      decimal b = 2.5m;
      decimal result = a * b;
      
      Console.WriteLine("Decimal a: " + a);
      Console.WriteLine("Decimal b: " + b);
      Console.WriteLine("Result: " + result);
      Console.WriteLine("Type: " + a.GetType());
   }
}

The output of the above code is −

Decimal a: 3.141592653589793238462643
Decimal b: 2.5
Result: 7.854481633974483096156607500
Type: System.Decimal

Comparison

Type Size Precision Range Suffix Best For
float 4 bytes 7 digits ±3.4 × 1038 f Graphics, games
double 8 bytes 15-17 digits ±1.7 × 10308 none Scientific calculations
decimal 16 bytes 28-29 digits ±7.9 × 1028 m Financial calculations

Precision and Memory Comparison float 4 bytes 7 digits Fast, less precise Graphics/games double 8 bytes 15-17 digits Default choice Scientific work decimal 16 bytes 28-29 digits Exact precision Financial data Memory usage increases with precision

Common Use Cases

  • float: Use when memory is limited and high precision isn't critical, such as graphics programming, game development, or embedded systems.

  • double: The default choice for most floating-point calculations, scientific computations, and general-purpose numeric operations.

  • decimal: Essential for financial calculations, currency operations, and any scenario where exact decimal representation is required.

Conclusion

Choose float for memory-constrained scenarios, double for general calculations, and decimal for financial operations. The key difference lies in precision and intended use: floating-point types (float/double) use binary representation, while decimal uses base-10 for exact decimal calculations.

Updated on: 2026-03-17T07:04:35+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements