C# Decimal Parse(String, NumberStyles, IFormatProvider)

Description

Decimal Parse(String, NumberStyles, IFormatProvider) converts the string representation of a number to its Decimal equivalent using the specified style and culture-specific format.

Syntax

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


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

Parameters

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

  • s - The string representation of the number to convert.
  • style - A bitwise combination of NumberStyles values that indicates the style elements that can be present in s. A typical value to specify is Number.
  • provider - An IFormatProvider object that supplies culture-specific information about the format of s.

Returns

Decimal.Parse(String, NumberStyles, IFormatProvider) method returns The Decimal number equivalent to the number contained in s as specified by style and provider.

Example

The following example uses a variety of style and provider parameters to parse the string representations of Decimal values.


using System;//  w ww .java2  s.c  o m
using System.Globalization;
public class MainClass{
  public static void Main(String[] argv){  
    string value;
    decimal number;
    NumberStyles style;
    CultureInfo provider;
    
    // Parse string using "." as the thousands separator 
    // and " " as the decimal separator. 
    value = "123 456,12";
    style = NumberStyles.AllowDecimalPoint | NumberStyles.AllowThousands;
    provider = new CultureInfo("fr-FR");
    
    number = Decimal.Parse(value, style, provider);  
    System.Console.WriteLine("'{0}' converted to {1}.", value, number);
    
    number = Decimal.Parse(value, style, CultureInfo.InvariantCulture);  
    System.Console.WriteLine("'{0}' converted to {1}.", value, number);
    
    // Parse string using "$" as the currency symbol for en-GB and  
    // en-us cultures. 
    value = "$6,543.51";
    style = NumberStyles.Number | NumberStyles.AllowCurrencySymbol;
    provider = new CultureInfo("en-GB");
    
    number = Decimal.Parse(value, style, provider);  
    System.Console.WriteLine("'{0}' converted to {1}.", value, number);
    
    
    provider = new CultureInfo("en-US");
    number = Decimal.Parse(value, style, provider);  
    System.Console.WriteLine("'{0}' converted to {1}.", value, 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