Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Comparison of double and float primitive types in C#
In C#, float and double are both floating-point data types used to store decimal numbers, but they differ significantly in precision, memory usage, and range. Understanding these differences is crucial for choosing the right data type for your applications.
Syntax
Following is the syntax for declaring float and double variables −
float floatVariable = 3.14f; double doubleVariable = 3.14159265359;
Note the f suffix for float literals and optional d suffix for double literals −
float price = 19.99f; double pi = 3.14159265359d; // 'd' is optional for double
Key Differences
| Property | float | double |
|---|---|---|
| Precision | Single precision (32-bit) | Double precision (64-bit) |
| Memory Size | 4 bytes | 8 bytes |
| Significant Digits | ~7 digits | ~15-17 digits |
| Range | -3.4 × 10³? to +3.4 × 10³? | ±5.0 × 10?³²? to ±1.7 × 10³?? |
| Default Value | 0.0f | 0.0d |
Precision Comparison Example
using System;
class Program {
public static void Main() {
float floatValue = 1.23456789f;
double doubleValue = 1.23456789012345;
Console.WriteLine("Float value: " + floatValue);
Console.WriteLine("Double value: " + doubleValue);
// Demonstrating precision loss
float largeFloat = 123456789.123456789f;
double largeDouble = 123456789.123456789;
Console.WriteLine("Large float: " + largeFloat);
Console.WriteLine("Large double: " + largeDouble);
}
}
The output of the above code is −
Float value: 1.234568 Double value: 1.23456789012345 Large float: 1.234568E+08 Large double: 123456789.123457
Mathematical Operations Example
using System;
class Program {
public static void Main() {
float a = 10.1f;
float b = 3.2f;
double c = 10.1;
double d = 3.2;
Console.WriteLine("Float division: " + (a / b));
Console.WriteLine("Double division: " + (c / d));
// Default values
float defaultFloat = default(float);
double defaultDouble = default(double);
Console.WriteLine("Default float: " + defaultFloat);
Console.WriteLine("Default double: " + defaultDouble);
}
}
The output of the above code is −
Float division: 3.15625 Double division: 3.15625 Default float: 0 Default double: 0
When to Use Each Type
Use float when −
Memory usage is a concern and moderate precision is acceptable
Working with graphics, games, or real-time applications
Interfacing with APIs that specifically require single-precision values
Use double when −
High precision calculations are required
Scientific computations, financial calculations, or engineering applications
Default choice for general-purpose floating-point operations
Conclusion
Float provides single-precision (32-bit) storage with ~7 significant digits, while double offers double-precision (64-bit) with ~15-17 significant digits. Choose double for high-precision requirements and float when memory efficiency is more important than precision.
