How to Convert a string to a double in C#
By FoxLearn 2/4/2025 6:32:01 AM 140
Use double.Parse()
double.Parse()
takes a string and converts it to a double-precision floating-point number. Here’s an example:
string input = "123.45"; double number = double.Parse(input);
When Conversion Fails
double.Parse()
throws an exception if it can't convert the string to a double. Here are a few examples of invalid input and the resulting exceptions:
//System.FormatException: Input string was not in a correct format. double.Parse("abc"); //System.OverflowException: Value was either too large or too small for a Double. double.Parse("1.7976931348623157E+308"); // Too large //System.ArgumentNullException: Value cannot be null. double.Parse(null);
You can handle these exceptions if you want to know why the conversion failed. Otherwise, use double.TryParse()
if you just want to check whether the conversion succeeds.
Use double.TryParse()
double.TryParse()
attempts to convert a string to a double. It doesn’t throw an exception on failure, instead it returns false
if it can't convert the string. If successful, it returns true
and sets the output parameter to the converted value.
Here’s an example of using double.TryParse()
:
string input = "123.45"; if (double.TryParse(input, out double number)) { // Use number Console.WriteLine($"Input * 2: {number * 2}"); } else { Console.WriteLine($"Couldn't convert {input} to a double"); }
This outputs:
Input * 2: 246.90
When conversion fails, the double
variable is set to 0.
double.TryParse("abc", out double number); Console.WriteLine(number);
This outputs 0
because it couldn't convert "abc"
to a double, so the default value of 0
is used. In most cases, you won’t want to use the result if the conversion failed.
Use Convert.ToDouble()
Convert.ToDouble()
can handle null
values and throws an exception if conversion fails. It behaves like double.Parse()
, but returns 0 when the string is null.
// 123.45 double number = Convert.ToDouble("123.45"); // 0 double number2 = Convert.ToDouble(null); Console.WriteLine(number2); // System.FormatException: Input string was not in a correct format Convert.ToDouble("abc");
Converting a Formatted String
If you need to convert a formatted string, like a currency or percentage, with double.Parse()
or double.TryParse()
, you’ll need to specify the NumberStyles
parameter. If not, the conversion will fail. Here’s an example of converting a formatted string with double.Parse()
:
using System.Globalization; double formattedValue = double.Parse(" $1,234.56 ", NumberStyles.Currency); Console.WriteLine($"Parsed double: {formattedValue}");
This outputs:
Parsed double: 1234.56
By default, this uses the current culture's formatting settings (CultureInfo.CurrentCulture).
- Using the OrderBy and OrderByDescending in LINQ
- Querying with LINQ
- Optimizing Performance with Compiled Queries in LINQ
- MinBy() and MaxBy() Extension Methods in .NET
- SortBy, FilterBy, and CombineBy in NET 9
- Exploring Hybrid Caching in .NET 9.0
- Using Entity Framework with IDbContext in .NET 9.0
- Primitive types in C#