This is a guide to Convert String to Double in C#. Both these methods return FormatException if the string argument doesn’t represent the number in a valid format.These methods take string representation of a number as input and return its equivalent double-precision floating-point number.String value can be converted to double using Convert.ToDouble() or Double.Parse() method.String strValues = ", strValues, doubleVal) On success, it returns true and the respected double value gets stored in the ‘out’ parameter.Įxamples of Convert String to Double in C#Įxample showing the conversion of string to double using Convert.ToDouble() method. Double.TryParse(String, Double) method works the same as Double.Parse() method except it returns a Boolean value which indicates whether the conversion has been done successfully or not.The difference is that if the string is “null” then this method does not return zero instead it returns ArgumentNullException. Double.Parse(String) method works similarly to Convert.ToDouble() method as it takes a string representation of a number as an argument and converts it to a double-precision floating-point number.In C#, there exists another common way of converting a string to double which can be done by using Double.Parse() method and Double.TryParse() method.If the value of the string in both of the above methods is “null” then these methods will return zero.Now, Convert.ToDouble() method will convert the given string to double using the formatting information provided by the object of NumberFormatInfo and the resultant value will be stored in the variable “doubleVal”.NumberGroupSeparator is used to get or set a string that separates groups of digits at the left of the decimal in numeric values.NumberDecimalSeparator is used to get or set a string that can be used as the decimal separator in numeric values.Then, we set some important properties for this object like NumberDecimalSeparator and NumberGroupSeparator.
In the above statements, we first created an object of IFormatProvider using the class NumberFormatInfo which implements IFormatProvider.
NumberFormatInfo provider = new NumberFormatInfo() ĭouble doubleVal = Convert.ToDouble("855.65", provider) Let us now understand the working of the above method with the help of the below example: “855.65” as a string to ToDouble() method which will be converted to double by the method and the resulted value will be stored in a variable of type double (doubleVal). In the above statement, we have passed a number i.e. Let us understand the working of the above method with the help of the below example:ĭouble doubleVal = Convert.ToDouble("855.65") Among these overloaded forms, two forms allow us to convert a string representation of a number to its equivalent double-precision floating-point number. In C#, the “System” namespace contains a class called “Convert” which contains the ToDouble() method in many overloaded forms to convert the specified type of data to its equivalent double value. After conversion both these methods return the equivalent double value for the string passed as the argument. In the above syntax, ToDouble() method takes two arguments the first is the string representation of a number which needs to be converted to double and second is an object which provides culture-specific formatting information.
Public static double ToDouble(string strValue, IFormatProvider provider) In the above syntax, ToDouble() method takes one argument of type string ( strValue) which is nothing but a string containing a number to convert to double. Public static double ToDouble(string strValue) The syntax of Convert.ToDouble() method in both its overloaded forms for converting a string to double is as follows:
Web development, programming languages, Software testing & othersīoth these methods return a double value after converting the string value to double.
Start Your Free Software Development Course