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
What are floating point literals in C#?
A floating-point literal in C# represents a decimal number that can contain a fractional part. It consists of an integer part, a decimal point, a fractional part, and optionally an exponent part. You can represent floating point literals in either decimal form or exponential form.
Syntax
Following are the different forms of floating-point literal syntax −
// Decimal form 123.45 0.5678 // Exponential form 1.23E+2 // 1.23 × 10² 4.56e-3 // 4.56 × 10?³ // With type suffixes 3.14f // float 2.718 // double (default) 1.414m // decimal
Types of Floating-Point Literals
C# supports three floating-point data types, each with specific suffixes −
| Type | Suffix | Range | Precision |
|---|---|---|---|
| float | f or F | ±1.5 × 10??? to ±3.4 × 10³? | 7 digits |
| double | d or D (optional) | ±5.0 × 10?³²? to ±1.7 × 10³?? | 15-16 digits |
| decimal | m or M | ±1.0 × 10?²? to ±7.9 × 10²? | 28-29 digits |
Using Decimal Form Literals
Example
using System;
class Program {
static void Main(string[] args) {
// float literals with 'f' suffix
float a = 3.56f;
float b = 3.14159f;
// double literals (default for decimal numbers)
double c = 9.23456;
double d = 0.123456789012345;
// decimal literals with 'm' suffix
decimal e = 123.456789m;
Console.WriteLine("Float a: " + a);
Console.WriteLine("Float b: " + b);
Console.WriteLine("Double c: " + c);
Console.WriteLine("Double d: " + d);
Console.WriteLine("Decimal e: " + e);
}
}
The output of the above code is −
Float a: 3.56 Float b: 3.14159 Double c: 9.23456 Double d: 0.123456789012345 Decimal e: 123.456789
Using Exponential Form Literals
Exponential notation uses 'E' or 'e' followed by an exponent to represent very large or very small numbers −
Example
using System;
class Program {
static void Main(string[] args) {
// Exponential form literals
double large = 1.23E+5; // 1.23 × 10? = 123000
double small = 4.56E-3; // 4.56 × 10?³ = 0.00456
float scientific = 2.99E8f; // Speed of light approximation
Console.WriteLine("Large number: " + large);
Console.WriteLine("Small number: " + small);
Console.WriteLine("Scientific notation: " + scientific);
// Mixed usage
double mixed1 = 269485E-5; // 2.69485
float mixed2 = 6.022E23f; // Avogadro's number approximation
Console.WriteLine("Mixed 1: " + mixed1);
Console.WriteLine("Mixed 2: " + mixed2);
}
}
The output of the above code is −
Large number: 123000 Small number: 0.00456 Scientific notation: 299000000 Mixed 1: 2.69485 Mixed 2: 6.022E+23
Common Use Cases
float: Used for graphics, gaming, and applications where memory is a concern
double: Default choice for most mathematical calculations and scientific computing
decimal: Used for financial calculations where precision is critical
Exponential form: Used for representing very large or very small numbers efficiently
Conclusion
Floating-point literals in C# provide flexible ways to represent decimal numbers using decimal or exponential notation. Choose the appropriate type (float, double, or decimal) based on your precision requirements and use the correct suffix to ensure proper type assignment.
