C# Double TryParse(String, NumberStyles, IFormatProvider, Double)

Description

Double TryParse(String, NumberStyles, IFormatProvider, Double) converts the string representation of a number in a specified style and culture-specific format to its double-precision floating-point number equivalent. A return value indicates whether the conversion succeeded or failed.

Syntax

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


public static bool TryParse(
  string s,// w w w  . j  ava2 s . c  om
  NumberStyles style,
  IFormatProvider provider,
  out double result
)

Parameters

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

  • s - A string containing a number to convert.
  • style - A bitwise combination of NumberStyles values that indicates the permitted format of s. A typical value to specify is NumberStyles.Float combined with NumberStyles.AllowThousands.
  • provider - An IFormatProvider that supplies culture-specific formatting information about s.
  • result - When this method returns, contains a double-precision floating-point number equivalent of the numeric value or symbol contained in s, if the conversion succeeded, or zero if the conversion failed. The conversion fails if the s parameter is null or String.Empty, is not in a format compliant with style, represents a number less than MinValue or greater than MaxValue, or if style is not a valid combination of NumberStyles enumerated constants. This parameter is passed uninitialized.

Returns

Double.TryParse(String, NumberStyles, IFormatProvider, Double) method returns true if s was converted successfully; otherwise, false.

Example

The following example demonstrates the use of the Double.TryParse(String, NumberStyles, IFormatProvider, Double) method to parse the string representation of numbers that have a particular style and are formatted using the conventions of a particular culture.


using System;//from  w  w  w.j  a v  a  2s  .c om
using System.Globalization;
public class MainClass{
  public static void Main(String[] argv){  
    string value;
    NumberStyles style;
    CultureInfo culture;
    double number;
    
    // Parse currency value using en-GB culture. 
    value = "-1,097.63";
    style = NumberStyles.Number | NumberStyles.AllowCurrencySymbol;
    culture = CultureInfo.CreateSpecificCulture("en-GB");
    if (Double.TryParse(value, style, culture, out number))
       Console.WriteLine("Converted '{0}' to {1}.", value, number);
    else
       Console.WriteLine("Unable to convert '{0}'.", value);
    
    
    value = "1345,978";
    style = NumberStyles.AllowDecimalPoint;
    culture = CultureInfo.CreateSpecificCulture("fr-FR");
    if (Double.TryParse(value, style, culture, out number))
       Console.WriteLine("Converted '{0}' to {1}.", value, number);
    else
       Console.WriteLine("Unable to convert '{0}'.", value);
    
    
    value = "1.345,978";
    style = NumberStyles.AllowDecimalPoint | NumberStyles.AllowThousands;
    culture = CultureInfo.CreateSpecificCulture("es-ES");
    if (Double.TryParse(value, style, culture, out number))
       Console.WriteLine("Converted '{0}' to {1}.", value, number);
    else
       Console.WriteLine("Unable to convert '{0}'.", value);
    
    
    value = "1 345,978";
    if (Double.TryParse(value, style, culture, out number))
       Console.WriteLine("Converted '{0}' to {1}.", value, number);
    else
       Console.WriteLine("Unable to convert '{0}'.", value);

  }
}

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