How to Resolve Error "CS0595: Invalid real literal" in C#
The Compiler Error CS0595 is a syntax error regarding Floating-Point Numbers. The message reads: "Invalid real literal".
In C#, a "real literal" refers to a number that represents a non-integer value (like double, float, or decimal). The compiler follows strict rules for how these numbers are written, including decimal points, exponents (scientific notation), and type suffixes. This error occurs when you start writing a number that looks like a floating-point value, but the syntax is incomplete, malformed, or contains conflicting characters.
This guide explains the correct syntax for real literals and how to fix common formatting mistakes.
Understanding Real Literals
A valid real literal in C# generally consists of:
- Digits:
0through9. - Decimal Point:
.(optional if exponent is present). - Exponent Part:
eorEfollowed by a sign (+/-) and digits. - Type Suffix:
f/F(float),d/D(double), orm/M(decimal).
Examples of Valid Literals:
1.5(double)3.14f(float)1.2e10(Scientific notation).5(Implicit zero)
CS0595 triggers when the compiler identifies a token as a number but cannot parse it according to these rules.
Scenario 1: Malformed Scientific Notation
Scientific notation requires an exponent value after the e or E. If you end the number immediately after the e, or if you include a sign without a number, the literal is invalid.
Example of error
public class MathCalc
{
public void Calculate()
{
// ⛔️ Error CS0595: Invalid real literal.
// The compiler sees 'e' and expects a power (like e2 or e-5).
// It finds a semicolon instead.
double val1 = 1.5e;
// ⛔️ Error CS0595: Exponent sign exists, but the number is missing.
double val2 = 3.2e+;
}
}
Solution: Complete the Exponent
Ensure every e is followed by an integer.
public class MathCalc
{
public void Calculate()
{
// ✅ Correct: 1.5 * 10^0 (which is 1.5)
// Or simply remove the 'e' if no exponent is needed.
double val1 = 1.5e0;
// ✅ Correct: 3.2 * 10^2 (320)
double val2 = 3.2e+2;
}
}
Scenario 2: Invalid or Conflicting Suffixes
You can use suffixes to specify the type (f, d, m). However, you cannot combine them (e.g., you cannot make a number both float and decimal), nor can you use invalid characters immediately after the number.
Example of error
public class Pricing
{
public void SetPrice()
{
// ⛔️ Error CS0595: Invalid real literal.
// You cannot combine 'f' (float) and 'm' (decimal).
var price = 10.5fm;
// ⛔️ Error CS0595: 'x' is not a valid suffix for a real literal.
// (If you meant hex 0x..., that syntax is for integers only).
var value = 1.5x;
}
}
Solution: Choose One Suffix
Select the correct type for your variable.
public class Pricing
{
public void SetPrice()
{
// ✅ Correct: Use 'm' for money (decimal)
decimal price = 10.5m;
// ✅ Correct: Use 'f' for float
float speed = 10.5f;
}
}
Scenario 3: Hexadecimal Floating-Point Errors
C# supports hexadecimal integer literals (e.g., 0xFF), but it does not support standard hexadecimal floating-point literals (like 0x1.5p10 found in C++ or Java).
If you try to write a hex number with a decimal point or a real suffix, CS0595 (or sometimes syntax error CS1002) occurs because C# treats 0x... strictly as an integer format.
Example of error
public class Color
{
public void Init()
{
// ⛔️ Error CS0595 / Syntax Error:
// C# thinks '0xFF' is an integer, then sees '.5' and gets confused.
// It does not parse this as a single number.
var val = 0xFF.5;
}
}
Solution: Use Converters
If you need to represent binary floating-point data, use BitConverter. If you just want the value, write it in decimal.
public class Color
{
public void Init()
{
// ✅ Correct: 0xFF is 255. So 255.5
double val = 255.5;
}
}
Conclusion
CS0595 means you broke the rules of writing numbers.
- Check Exponents: If you use
e, make sure a number follows it (1.2e5). - Check Suffixes: Use only one suffix at a time (
f,d,m). - Check Hex: Remember that
0xis only for integers in C#.