C# Double Parse(String, NumberStyles, IFormatProvider)

Description

Double Parse(String, NumberStyles, IFormatProvider) converts the string representation of a number in a specified style and culture-specific format to its double-precision floating-point number equivalent.

Syntax

Double.Parse(String, NumberStyles, IFormatProvider) has the following syntax.


public static double Parse(
  string s,/* ww w.  j a v  a  2 s  . c o  m*/
  NumberStyles style,
  IFormatProvider provider
)

Parameters

Double.Parse(String, NumberStyles, IFormatProvider) has the following parameters.

  • s - A string that contains a number to convert.
  • style - A bitwise combination of enumeration values that indicate the style elements that can be present in s. A typical value to specify is Float combined with AllowThousands.
  • provider - An object that supplies culture-specific formatting information about s.

Returns

Double.Parse(String, NumberStyles, IFormatProvider) method returns A double-precision floating-point number that is equivalent to the numeric value or symbol specified in s.

Example

The following example illustrates the use of the Parse(String, NumberStyles, IFormatProvider) method to parse a string value to double.


/*from ww  w.  j  a  v a 2  s. c o  m*/
using System;
using System.Globalization;

public class Temperature
{
   public static void Main()
   {
      string value;
      NumberStyles styles;
      IFormatProvider provider;
      Temperature temp;

      value = "25,3";
      styles = NumberStyles.Float;
      provider = CultureInfo.CreateSpecificCulture("fr-FR");
      double d = Double.Parse(value, styles, provider);


      value = " (40)";
      styles = NumberStyles.AllowLeadingWhite | NumberStyles.AllowTrailingWhite 
               | NumberStyles.AllowParentheses;
      provider = NumberFormatInfo.InvariantInfo;
       d = Double.Parse(value, styles, provider);


      value = "5,778E03";      // Approximate surface temperature of the Sun
      styles = NumberStyles.AllowDecimalPoint | NumberStyles.AllowThousands |
               NumberStyles.AllowExponent;
      provider = CultureInfo.CreateSpecificCulture("en-GB"); 
       d = Double.Parse(value, styles, provider);

   }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System »




Array
BitConverter
Boolean
Byte
Char
Console
ConsoleKeyInfo
Convert
DateTime
DateTimeOffset
Decimal
Double
Enum
Environment
Exception
Guid
Int16
Int32
Int64
Math
OperatingSystem
Random
SByte
Single
String
StringComparer
TimeSpan
TimeZone
TimeZoneInfo
Tuple
Tuple
Tuple
Type
UInt16
UInt32
UInt64
Uri
Version