How to Convert a String to a double with double.Parse in C#
double.Parse("3.5", CultureInfo.InvariantCulture) turns the text "3.5" into the number 3.5. The culture tells it which character starts the fraction, and with the German culture, the same text became 35.
Microsoft's reference: Converts the string representation of a number to its double-precision floating-point number equivalent. A double-precision floating-point number is a double, the type C# uses for numbers with a fraction, such as 3.5.
The text has to show where the fraction starts, and that depends on the culture: English writes a full stop, German a comma.
Syntax
double <number> = double.Parse(<text>, <culture>);
| Part | What it is |
|---|---|
<text> | The string to convert, such as "3.5" |
<culture> | The culture the text was written in, such as CultureInfo.InvariantCulture for a full stop before the decimals. It needs using System.Globalization; |
<number> | The double you get back |
Which text works, in the reference's words: The s parameter is interpreted using a combination of the NumberStyles.Float and NumberStyles.AllowThousands flags. This means that white space and thousands separators are allowed, for example, while currency symbols are not.
The Example
A console app called DoubleParse adds two distances that arrive as text, "3.5" and "1.25" kilometres, and then reads "3,5" and "3.5" with the German culture.
Prerequisites
- The .NET 10 SDK.
dotnet --versionprints a version that starts with10.0.
Step 1: Create a Console App
dotnet new console -n DoubleParse
cd DoubleParse
The template "Console App" was created successfully.
Processing post-creation actions...
Restoring /Users/you/source/DoubleParse/DoubleParse.csproj:
Determining projects to restore...
Restored /Users/you/source/DoubleParse/DoubleParse.csproj (in 41 ms).
Restore succeeded.
dotnet new console -n DoubleParse creates a folder named DoubleParse with a project file and a Program.cs, and cd moves into it. Everything this example uses is part of .NET, so there's no package to add. Open the folder in VS Code, or DoubleParse.csproj in Visual Studio.
Step 2: Write the Code
Replace everything in Program.cs. First the cultures and the distances:
using System.Globalization;
CultureInfo invariant = CultureInfo.InvariantCulture;
CultureInfo german = CultureInfo.GetCultureInfo("de-DE");
// Two distances in kilometres that arrive as text.
double morning = double.Parse("3.5", invariant);
double evening = double.Parse("1.25", invariant);
double total = morning + evening;
Console.WriteLine($"Total: {total.ToString(invariant)} km");
CultureInfo.InvariantCultureis a culture that belongs to no country and writes a full stop before the decimals.CultureInfo.GetCultureInfo("de-DE")is German as written in Germany.double.Parse("3.5", invariant)reads the text as 3.5, andmorning + eveningadds twodoublevalues.total.ToString(invariant)writes the result with a full stop on every computer.
Then the German culture:
// In German, a comma separates the decimals.
double fromGerman = double.Parse("3,5", german);
Console.WriteLine($"3,5 read as German: {fromGerman.ToString(invariant)}");
// Read as German, the full stop in 3.5 is a thousands separator.
double misread = double.Parse("3.5", german);
Console.WriteLine($"3.5 read as German: {misread.ToString(invariant)}");
The same method reads "3,5" and "3.5", this time with the German culture.
Step 3: Run It
Run it in VS Code's terminal, or press Ctrl+F5 in Visual Studio:
dotnet run
Total: 4.75 km
3,5 read as German: 3.5
3.5 read as German: 35

- 3.5 and 1.25 added up to 4.75: the text became numbers.
"3,5"with the German culture gave 3.5."3.5"with the German culture gave 35, without any error. In the reference's list of what the text may hold,,is A culture-specific thousands separator symbol. and.is A culture-specific decimal point symbol. German uses them the other way round, so the full stop was read as a thousands separator.
The Complete File

using System.Globalization;
CultureInfo invariant = CultureInfo.InvariantCulture;
CultureInfo german = CultureInfo.GetCultureInfo("de-DE");
// Two distances in kilometres that arrive as text.
double morning = double.Parse("3.5", invariant);
double evening = double.Parse("1.25", invariant);
double total = morning + evening;
Console.WriteLine($"Total: {total.ToString(invariant)} km");
// In German, a comma separates the decimals.
double fromGerman = double.Parse("3,5", german);
Console.WriteLine($"3,5 read as German: {fromGerman.ToString(invariant)}");
// Read as German, the full stop in 3.5 is a thousands separator.
double misread = double.Parse("3.5", german);
Console.WriteLine($"3.5 read as German: {misread.ToString(invariant)}");
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
| Leaving out the culture | The computer's culture decides: with the current culture set to de-DE, double.Parse("3.5") gave 35 | Pass the culture the text was written in. For text from files and other programs, CultureInfo.InvariantCulture gave 3.5 |
Text with a currency symbol, such as "$4.99" | FormatException: The input string '$4.99' was not in a correct format. Currency symbols aren't allowed by default | Allow them with NumberStyles.Currency and a culture that uses $: double.Parse("$4.99", NumberStyles.Currency, CultureInfo.GetCultureInfo("en-US")) gave 4.99 |
| Catching an exception for a number that's too big | There is none: "1e400" gave Infinity. The reference: In .NET Core 3.0 and later, values that are too large to represent are rounded to PositiveInfinity or NegativeInfinity as required by the IEEE 754 specification. | Check the result with double.IsInfinity, which returned True |
FAQ
Which text does double.Parse accept?
Digits with one decimal separator, plus spaces around the number, a sign, thousands separators and an exponent. With the invariant culture, " -0.75 " gave -0.75, "1,250.5" gave 1250.5, and "2.5e3" gave 2500. Text that isn't a number, such as "abc", threw FormatException: The input string 'abc' was not in a correct format.
Does a double written with ToString read back the same?
Yes, with the same culture on both sides. 0.1 + 0.2 written with ToString(CultureInfo.InvariantCulture) was 0.30000000000000004, and double.Parse of that text gave the same value back. The reference: Ordinarily, if you pass the Double.Parse method a string that is created by calling the Double.ToString method, the original Double value is returned.
Conclusion
Read numbers with a fraction with double.Parse(text, culture), and pass the culture the text was written in: CultureInfo.InvariantCulture for a full stop, or a culture such as de-DE for a comma. With the wrong culture, "3.5" became 35 without any error.
Additional Resources
- Double.Parse — every overload, and the text each one accepts.
- NumberStyles — the styles that allow currency symbols and more.
- CultureInfo.InvariantCulture — the culture that is the same on every computer.
