The Compiler Error CS0664 is a type mismatch error involving numeric literals. The message reads: "Literal of type double cannot be implicitly converted to type 'decimal' (or float); use an 'M' (or 'F') suffix to create a literal of this type".
In C#, any numeric literal containing a decimal point (e.g., 10.5) is automatically treated as a double (64-bit floating-point).
float(32-bit floating-point) has less precision thandouble.decimal(128-bit decimal floating-point) has a completely different memory layout and handling mechanism thandouble.
Because converting a double to float loses precision, and converting double to decimal changes the fundamental type representation, C# refuses to do these conversions automatically (implicitly). You must explicitly tell the compiler the type of the literal using a Suffix.
Understanding Numeric Suffixes
To specify the type of a numeric literal, append a specific letter to the number:
| Suffix | Type | Example | Usage |
|---|---|---|---|
| None | double |
10.5 |
Scientific calculations, default math. |
f or F |
float |
10.5f |
Graphics, games (Unity), lower precision memory. |
m or M |
decimal |
10.5m |
Financial calculations, currency. |
Scenario 1: Assigning to Decimal
This is the most common occurrence, especially in financial applications. You define a price variable as decimal but try to assign 19.99 to it.
Example of error
public class Product
{
// ⛔️ Error CS0664: Literal of type double cannot be implicitly converted to type 'decimal';
// use an 'M' suffix to create a literal of this type.
public decimal Price = 19.99;
}Solution: Use the 'M' Suffix
Append m or M to the number.
public class Product
{
// ✅ Correct: The 'm' tells the compiler this is a decimal literal.
public decimal Price = 19.99m;
}Scenario 2: Assigning to Float
This is frequently seen in game development (like Unity) or graphics programming where float is the standard type for coordinates.
Example of error
public class Player
{
// ⛔️ Error CS0664: Literal of type double cannot be implicitly converted to type 'float'.
// 1.5 is a double (64-bit). float is 32-bit.
public float Speed = 1.5;
}Solution: Use the 'F' Suffix
Append f or F.
public class Player
{
// ✅ Correct: The 'f' suffix defines a single-precision float.
public float Speed = 1.5f;
}Alternative Solution: Explicit Cast
You can also cast, though the suffix is cleaner.
// Valid, but verbose
public float Speed = (float)1.5;Conclusion
CS0664 is the compiler asking you to be specific about precision.
- Check the Variable Type: Is it
decimalorfloat? - Add the Suffix:
- For
decimal, addm(e.g.,100.0m). - For
float, addf(e.g.,100.0f).
- For