float, double, and decimal in C#?float is a 32-bit floating point type, double is 64-bit, and decimal is 128-bit with higher precision for financial calculations.
float and double are binary floating point types, while decimal is a decimal floating point type, reducing rounding errors in money calculations.
float variable with the value 3.14 in C#?You write: float pi = 3.14f;
The f suffix tells the compiler this is a float literal, not a double.
decimal preferred for financial calculations over float or double?decimal has higher precision and uses base-10 representation, which reduces rounding errors common in binary floating point types like float and double.
This makes decimal better for money where exact decimal values matter.
3.14 in C#?The default type is double.
If you want a float, you must add f or F suffix, and for decimal, use m or M.
double value to a float variable without casting?The compiler will give an error because double has more precision and size than float.
You must explicitly cast it like float f = (float)myDouble; to avoid errors.
decimal has 128-bit precision and is designed for high-precision decimal calculations.
float literal in C#?The suffix f or F marks a literal as float.
5.0 in C#?Floating point literals without suffix are double by default.
decimal is designed for precise decimal arithmetic, ideal for money.
double value to a float variable?You must explicitly cast because double is larger than float.
float, double, and decimal types in C# and when to use each.