What we saw on each .NET version
The same calls, run on .NET 6, 7, 8 and 10:
| Call | .NET 6 | .NET 7 and later |
|---|---|---|
int.Parse("12x") | Input string was not in a correct format. | The input string '12x' was not in a correct format. |
int.Parse("") | Input string was not in a correct format. | The input string '' was not in a correct format. |
Convert.ToInt32("1.5") | Input string was not in a correct format. | The input string '1.5' was not in a correct format. |
decimal.Parse("1,234.50", de-DE) | Input string was not in a correct format. | The input string '1,234.50' was not in a correct format. |
Common causes
- Empty or whitespace-only text, often an empty form field or a missing CSV column. Surrounding spaces alone are fine: the default styles allow them.
- A decimal number parsed as an integer.
int.Parse("1.5")fails; parse as decimal and round, or fix the data. - The wrong culture. Without a CultureInfo, parsing uses the current culture of the thread, which is the server's or the user's. "1,234.50" is valid in en-US and invalid in de-DE, where "," is the decimal separator.
- Currency symbols, percent signs or units in the text, which need NumberStyles.Currency or trimming first.
Parse safely
using System.Globalization;
// TryParse returns false instead of throwing.
Console.WriteLine(int.TryParse("12x", out int quantity)); // prints False
Console.WriteLine(int.TryParse(" 42 ", out quantity)); // prints True
// The culture decides what "," and "." mean.
var english = CultureInfo.GetCultureInfo("en-US");
var german = CultureInfo.GetCultureInfo("de-DE");
Console.WriteLine(decimal.Parse("1,234.50", english).ToString(CultureInfo.InvariantCulture)); // prints 1234.50
Console.WriteLine(decimal.Parse("1.234,50", german).ToString(CultureInfo.InvariantCulture)); // prints 1234.50
Console.WriteLine(decimal.TryParse("1,234.50", NumberStyles.Number, german, out _)); // prints False
// An integer type rejects a decimal point.
Console.WriteLine(int.TryParse("1.5", NumberStyles.Integer, CultureInfo.InvariantCulture, out _)); // prints False
FAQ
What causes "Input string was not in a correct format"?
A number parse got text it cannot read: an empty string, letters, a decimal point for an integer type, or thousands and decimal separators from a different culture than the one used to parse.
Why does my message say "The input string 'abc' was not in a correct format"?
Since .NET 7 the message quotes the text that failed. .NET 6 and .NET Framework say "Input string was not in a correct format." without it. It is the same exception.
How do I avoid the FormatException?
Use int.TryParse (or decimal.TryParse, and so on) for text you do not control, and pass a CultureInfo: CultureInfo.InvariantCulture for machine formats such as JSON, config files and CSV exports.
Can string.Format throw a FormatException too?
Yes, with a different message: "Index (zero based) must be greater than or equal to zero and less than the size of the argument list." That one means the format string has a placeholder such as {1} without a matching argument.