Skip to content

How to Resolve Error "CS1013: Invalid number" in C#

Learn how to fix the C# compiler error CS1013 when encountering malformed numeric literals, including hexadecimal, scientific notation, and decimal typos.

The Compiler Error CS1013 is a lexical analysis error. The message reads: "Invalid number".

This error occurs when the C# compiler encounters a numeric literal that violates the syntax rules for numbers. It recognizes that you are trying to write a number (because it starts with a digit, a decimal point, or a format specifier like 0x), but the characters following it do not form a valid value. This is often caused by incomplete hexadecimal values, broken scientific notation, or typos in decimal placement.

This guide explains the correct syntax for various numeric formats in C# and how to fix malformed literals.

Understanding Numeric Literal Syntax

C# supports several ways to write numbers:

  • Decimal: 123, 10.5
  • Hexadecimal: Starts with 0x (e.g., 0xFF).
  • Binary: Starts with 0b (e.g., 0b1010).
  • Scientific: Uses e or E (e.g., 1.5e2).

The error CS1013 implies the start of the number looked correct, but the ending was incomplete or contained invalid characters.

Scenario 1: Malformed Hexadecimal Literals

In C#, a hexadecimal number must start with 0x and be followed immediately by at least one valid hex digit (0-9, A-F). If you type 0x and stop, or follow it with a non-hex character (like G or a space), the number is invalid.

Example of error

Leaving the 0x prefix hanging without a value.

public class ColorConfig
{
    // ⛔️ Error CS1013: Invalid number
    // The compiler sees the prefix '0x', but finds a semicolon immediately after.
    // It requires actual digits to form a number.
    public int Red = 0x; 
}

Solution: Complete the Literal

Provide the hexadecimal digits.

public class ColorConfig
{
    // ✅ Correct: '0x0' is valid (Decimal 0)
    public int Red = 0x0; 

    // ✅ Correct: '0xFF' is valid (Decimal 255)
    public int Blue = 0xFF; 
}

Scenario 2: Broken Scientific Notation

Scientific notation is used for floating-point literals. It requires three parts:

  1. The base number (e.g., 1.5).
  2. The exponent indicator (e or E).
  3. The exponent value (an integer, e.g., 2 or -5).

If you type the e but forget the exponent, the number is incomplete.

Example of error

public void CalculatePhysics()
{
    // ⛔️ Error CS1013: Invalid number
    // The 'e' indicates an exponent follows, but the statement ends.
    double distinct = 1.5e; 
    
    // ⛔️ Error CS1013: Even with a sign, you need a number after it.
    double distance = 4.2e+;
}

Solution: Add the Exponent

Specify the power of 10.

public void CalculatePhysics()
{
    // ✅ Correct: 1.5 * 10^2 = 150.0
    double distinct = 1.5e2; 

    // ✅ Correct: 4.2 * 10^5 = 420,000.0
    double distance = 4.2e+5;
}

Scenario 3: Typographical Errors in Decimals

While C# allows numbers to start with a decimal point (e.g., .5), or end with one in some contexts (though 1. is usually valid as a double), typing double dots or accidental characters inside the number causes format errors.

Example of error

Accidentally typing two decimal points.

public class Product
{
    // ⛔️ Error CS1013: Invalid number
    // A number cannot have two decimal points.
    public double Price = 10..99; 
}

Solution: Fix the Format

Correct the typo to use standard decimal syntax.

public class Product
{
    // ✅ Correct
    public double Price = 10.99; 
}

Conclusion

CS1013 is a strict syntax check on numeric literals.

  1. Check Hex: Does your number start with 0x? Ensure valid characters (0-9, A-F) follow it immediately.
  2. Check Scientific: Does your number contain e? Ensure an integer follows it.
  3. Check Typos: Ensure you haven't double-typed a decimal point or inserted a letter into a decimal number.
Navigation