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

Description

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

Syntax

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


public static bool TryParse(
  string s,/*from  ww  w  .jav  a  2  s. c om*/
  NumberStyles style,
  IFormatProvider provider,
  out float result
)

Parameters

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

  • s - A string representing a number to convert.
  • style - A bitwise combination of enumeration values that indicates the permitted format of s. A typical value to specify is NumberStyles.Float combined with NumberStyles.AllowThousands.
  • provider - An object that supplies culture-specific formatting information about s.
  • result - When this method returns, contains the single-precision floating-point number equivalent to 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 Single.MinValue or greater than Single.MaxValue, or if style is not a valid combination of NumberStyles enumerated constants. This parameter is passed uninitialized.

Returns

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

Example

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


/*from   ww  w .ja v a2 s  .c o  m*/
using System;
public class MainClass{
  public static void Main(String[] argv){  
    string value;
    System.Globalization.NumberStyles style;
    System.Globalization.CultureInfo culture;
    float number;
    
    // Parse currency value using en-GB culture. 
    value = "12,345.67";
    style = System.Globalization.NumberStyles.Number | 
            System.Globalization.NumberStyles.AllowCurrencySymbol;
    culture = System.Globalization.CultureInfo.CreateSpecificCulture("en-GB");
    if (Single.TryParse(value, style, culture, out number))
       Console.WriteLine("Converted '{0}' to {1}.", value, number);
    else
       Console.WriteLine("Unable to convert '{0}'.", value);
    
    value = "1234,567";
    style = System.Globalization.NumberStyles.AllowDecimalPoint;
    culture = System.Globalization.CultureInfo.CreateSpecificCulture("fr-FR");
    if (Single.TryParse(value, style, culture, out number))
       Console.WriteLine("Converted '{0}' to {1}.", value, number);
    else
       Console.WriteLine("Unable to convert '{0}'.", value);
    
    value = "1.234,567";
    style = System.Globalization.NumberStyles.AllowDecimalPoint | 
            System.Globalization.NumberStyles.AllowThousands;
    culture = System.Globalization.CultureInfo.CreateSpecificCulture("es-ES");
    if (Single.TryParse(value, style, culture, out number))
       Console.WriteLine("Converted '{0}' to {1}.", value, number);
    else
       Console.WriteLine("Unable to convert '{0}'.", value);
    
    value = "1 234,567";
    if (Single.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