C# Single Parse(String, NumberStyles, IFormatProvider)

Description

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

Syntax

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


public static float Parse(
  string s,// www  .  j  a va 2 s.  c  o  m
  NumberStyles style,
  IFormatProvider provider
)

Parameters

Single.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 indicates 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

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

Example

The following code example uses the Parse(String, NumberStyles, IFormatProvider) method to parse the string representations of Single values.


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

public class Example
{
    public static void Main()
    {
      string[] values = { " 987.654E-2", " 987,654E-2",  "(98765,43210)", 
                          "9,876,543.210", "9.876.543,210",  "98_76_54_32,19" };
      CultureInfo ci = new CultureInfo("");
      ci.NumberFormat.NumberGroupSizes = new int[] { 2 };
      ci.NumberFormat.NumberGroupSeparator = "_";

      // Define an array of format providers.
      CultureInfo[] providers = { new CultureInfo("en-US"),
                                  new CultureInfo("nl-NL"), ci };       


      NumberStyles[] styles = { NumberStyles.Currency, NumberStyles.Float };

      foreach (CultureInfo provider in providers)
      {
         foreach (string value in values)
         {
            foreach (NumberStyles style in styles)
            {
                  float number = Single.Parse(value, style, provider);            
                  Console.WriteLine("   {0} ({1}) -> {2}", 
                                    value, style, number);
            }            
         }         
      }
   }   
} 

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