Converting a string to a double in C# is a common task when dealing with numerical data. There are several methods to convert string to double in C#. In this tutorial, we will go through different methods to convert a string to a double in C#.Net.
How to Convert String to Double in C#.Net
Here are the different methods to convert string to double in C#.Net.
Method 1: Using Double.Parse()
Double.Parse()
is a method that converts a string representation of a number to its double-precision floating-point equivalent in C#.Net.
Example:
using System;
class Program
{
static void Main()
{
string strNumber = "123.45";
double doubleNumber = Double.Parse(strNumber);
Console.WriteLine(doubleNumber);
}
}
This program converts the string “123.45” to a double and prints it to the console. You can see the output like below:
Method 2: Using Convert.ToDouble()
You can also use the Convert.ToDouble()
method in C#.Net which is a part of the System
namespace. This method is similar to Double.Parse()
but provides a unified approach for converting different types to double.
Example:
using System;
class Program
{
static void Main()
{
string strNumber = "123.45";
double doubleNumber = Convert.ToDouble(strNumber);
Console.WriteLine(doubleNumber);
}
}
Method 3: Using Double.TryParse()
The Double.TryParse()
method is similar to Double.Parse()
, but it does not throw an exception if the conversion fails. Instead, it returns a boolean value indicating whether the conversion succeeded or not.
Example:
using System;
class Program
{
static void Main()
{
string strNumber = "123.45";
double doubleNumber;
bool isParsed = Double.TryParse(strNumber, out doubleNumber);
if (isParsed)
{
Console.WriteLine(doubleNumber);
}
else
{
Console.WriteLine("Conversion Failed.");
}
}
}
Method 4: Using Invariant Culture
Sometimes the string representation of a double might have a different format depending on the culture (e.g. using a comma instead of a period as a decimal separator). You can use the Double.Parse()
method with the CultureInfo.InvariantCulture
to ensure that the conversion is culture-independent.
Example:
using System;
using System.Globalization;
class Program
{
static void Main()
{
string strNumber = "123,45";
double doubleNumber = Double.Parse(strNumber, CultureInfo.InvariantCulture);
Console.WriteLine(doubleNumber);
}
}
Method 5: Using Custom NumberStyles
If your string representation has custom formats, you can specify custom NumberStyles
to handle different number representations.
Example:
using System;
using System.Globalization;
class Program
{
static void Main()
{
string strNumber = "123.45";
double doubleNumber = Double.Parse(strNumber, NumberStyles.AllowDecimalPoint | NumberStyles.AllowThousands, CultureInfo.InvariantCulture);
Console.WriteLine(doubleNumber);
}
}
Conclusion
In this tutorial, we explored different ways to convert a string to a double in C#.Net. The method you choose depends on the specific requirements of your application. In general, Double.Parse()
and Convert.ToDouble()
are the most straightforward options, while Double.TryParse()
is useful for error handling, and custom NumberStyles
are useful for parsing strings in specific formats.
You may also like:
- Check if a String is Not Null or Empty in C#.NET
- How to return an empty string instead of a null in C#.Net?
- Check for Null Values in C#.NET
- Convert Object to JSON Without Escape Characters in C#.NET
- How to convert number with comma into decimal in C#?
Bijay Kumar is a renowned software engineer, accomplished author, and distinguished Microsoft Most Valuable Professional (MVP) specializing in SharePoint. With a rich professional background spanning over 15 years, Bijay has established himself as an authority in the field of information technology. He possesses unparalleled expertise in multiple programming languages and technologies such as ASP.NET, ASP.NET MVC, C#.NET, and SharePoint, which has enabled him to develop innovative and cutting-edge solutions for clients across the globe. Read more…