Decimal.Parse Method

Definition

Converts the string representation of a number to its Decimal equivalent.

Overloads

Parse(String)

Converts the string representation of a number to its Decimal equivalent.

Parse(ReadOnlySpan<Byte>, IFormatProvider)

Parses a span of UTF-8 characters into a value.

Parse(ReadOnlySpan<Char>, IFormatProvider)

Parses a span of characters into a value.

Parse(String, NumberStyles)

Converts the string representation of a number in a specified style to its Decimal equivalent.

Parse(String, IFormatProvider)

Converts the string representation of a number to its Decimal equivalent using the specified culture-specific format information.

Parse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider)

Parses a span of UTF-8 characters into a value.

Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider)

Converts the span representation of a number to its Decimal equivalent using the specified style and culture-specific format.

Parse(String, NumberStyles, IFormatProvider)

Converts the string representation of a number to its Decimal equivalent using the specified style and culture-specific format.

Parse(String)

Converts the string representation of a number to its Decimal equivalent.

public:
 static System::Decimal Parse(System::String ^ s);
public static decimal Parse (string s);
static member Parse : string -> decimal
Public Shared Function Parse (s As String) As Decimal

Parameters

s
String

The string representation of the number to convert.

Returns

The equivalent to the number contained in s.

Exceptions

s is not in the correct format.

s represents a number less than Decimal.MinValue or greater than Decimal.MaxValue.

Examples

The following code example uses the Parse(String) method to parse string representations of Decimal values.

string value;
decimal number;
// Parse an integer with thousands separators.
value = "16,523,421";
number = Decimal.Parse(value);
Console.WriteLine("'{0}' converted to {1}.", value, number);
// Displays:
//    '16,523,421' converted to 16523421.

// Parse a floating point value with thousands separators
value = "25,162.1378";
number = Decimal.Parse(value);
Console.WriteLine("'{0}' converted to {1}.", value, number);
// Displays:
//    '25,162.1378' converted to 25162.1378.

// Parse a floating point number with US currency symbol.
value = "$16,321,421.75";
try
{
   number = Decimal.Parse(value);
   Console.WriteLine("'{0}' converted to {1}.", value, number);
}
catch (FormatException)
{
   Console.WriteLine("Unable to parse '{0}'.", value);
}
// Displays:
//    Unable to parse '$16,321,421.75'.

// Parse a number in exponential notation
value = "1.62345e-02";
try
{
   number = Decimal.Parse(value);
   Console.WriteLine("'{0}' converted to {1}.", value, number);
}
catch (FormatException)
{
   Console.WriteLine("Unable to parse '{0}'.", value);
}
// Displays:
//    Unable to parse '1.62345e-02'.
// Parse an integer with thousands separators.
let value = "16,523,421"
let number = Decimal.Parse value
printfn $"'{value}' converted to {number}."
// Displays:
//    '16,523,421' converted to 16523421.

// Parse a floating point value with thousands separators
let value = "25,162.1378"
let number = Decimal.Parse value
printfn $"'{value}' converted to {number}."
// Displays:
//    '25,162.1378' converted to 25162.1378.

// Parse a floating point number with US currency symbol.
let value = "$16,321,421.75"
try
    let number = Decimal.Parse value
    printfn $"'{value}' converted to {number}."
with :? FormatException ->
    printfn $"Unable to parse '{value}'."
// Displays:
//    Unable to parse '$16,321,421.75'.

// Parse a number in exponential notation
let value = "1.62345e-02"
try
    let number = Decimal.Parse value
    printfn $"'{value}' converted to {number}."
with :? FormatException ->
    printfn $"Unable to parse '{value}'."
// Displays:
//    Unable to parse '1.62345e-02'.
Dim value As String
Dim number As Decimal

' Parse an integer with thousands separators. 
value = "16,523,421"
number = Decimal.Parse(value)
Console.WriteLine("'{0}' converted to {1}.", value, number)
' Displays: 
'    '16,523,421' converted to 16523421.

' Parse a floating point value with thousands separators
value = "25,162.1378"
number = Decimal.Parse(value)
Console.WriteLine("'{0}' converted to {1}.", value, number)
' Displays:
'    '25,162.1378' converted to 25162.1378.

' Parse a floating point number with US currency symbol.
value = "$16,321,421.75"
Try
   number = Decimal.Parse(value)
   Console.WriteLine("'{0}' converted to {1}.", value, number)
Catch e As FormatException
   Console.WriteLine("Unable to parse '{0}'.", value)
End Try
' Displays:
'    Unable to parse '$16,321,421.75'.  

' Parse a number in exponential notation
value = "1.62345e-02"
Try
   number = Decimal.Parse(value)
   Console.WriteLine("'{0}' converted to {1}.", value, number)
Catch e As FormatException
   Console.WriteLine("Unable to parse '{0}'.", value)
End Try
' Displays: 
'    Unable to parse '1.62345e-02'.

Remarks

Parameter s contains a number of the form:

[ws][sign][digits,]digits[.fractional-digits][ws]

Elements in square brackets ([ and ]) are optional. The following table describes each element.

Element Description
ws Optional white space.
sign An optional sign.
digits A sequence of digits ranging from 0 to 9.
, A culture-specific thousands separator symbol.
. A culture-specific decimal point symbol.
fractional-digits A sequence of digits ranging from 0 to 9.

Parameter s is interpreted using the NumberStyles.Number style. This means that white space and thousands separators are allowed but currency symbols are not. To explicitly define the elements (such as currency symbols, thousands separators, and white space) that can be present in s, use either the Decimal.Parse(String, NumberStyles) or the Decimal.Parse(String, NumberStyles, IFormatProvider) method.

Parameter s is parsed using the formatting information in a NumberFormatInfo initialized for the current system culture. For more information, see CurrentInfo. To parse a string using the formatting information of some other culture, use the Decimal.Parse(String, IFormatProvider) or Decimal.Parse(String, NumberStyles, IFormatProvider) method.

If necessary, the value of s is rounded using rounding to nearest.

A Decimal has 29 digits of precision. If s represents a number that has more than 29 digits, but has a fractional part and is within the range of MaxValue and MinValue, the number is rounded, not truncated, to 29 digits using rounding to nearest.

If during a parse operation a separator is encountered in the s parameter, and the applicable currency or number decimal and group separators are the same, the parse operation assumes that the separator is a decimal separator rather than a group separator. For more information about separators, see CurrencyDecimalSeparator, NumberDecimalSeparator, CurrencyGroupSeparator, and NumberGroupSeparator.

See also

Applies to

Parse(ReadOnlySpan<Byte>, IFormatProvider)

Parses a span of UTF-8 characters into a value.

public:
 static System::Decimal Parse(ReadOnlySpan<System::Byte> utf8Text, IFormatProvider ^ provider) = IUtf8SpanParsable<System::Decimal>::Parse;
public static decimal Parse (ReadOnlySpan<byte> utf8Text, IFormatProvider? provider);
static member Parse : ReadOnlySpan<byte> * IFormatProvider -> decimal
Public Shared Function Parse (utf8Text As ReadOnlySpan(Of Byte), provider As IFormatProvider) As Decimal

Parameters

utf8Text
ReadOnlySpan<Byte>

The span of UTF-8 characters to parse.

provider
IFormatProvider

An object that provides culture-specific formatting information about utf8Text.

Returns

The result of parsing utf8Text.

Implements

Applies to

Parse(ReadOnlySpan<Char>, IFormatProvider)

Parses a span of characters into a value.

public:
 static System::Decimal Parse(ReadOnlySpan<char> s, IFormatProvider ^ provider) = ISpanParsable<System::Decimal>::Parse;
public static decimal Parse (ReadOnlySpan<char> s, IFormatProvider? provider);
static member Parse : ReadOnlySpan<char> * IFormatProvider -> decimal
Public Shared Function Parse (s As ReadOnlySpan(Of Char), provider As IFormatProvider) As Decimal

Parameters

s
ReadOnlySpan<Char>

The span of characters to parse.

provider
IFormatProvider

An object that provides culture-specific formatting information about s.

Returns

The result of parsing s.

Implements

Applies to

Parse(String, NumberStyles)

Converts the string representation of a number in a specified style to its Decimal equivalent.

public:
 static System::Decimal Parse(System::String ^ s, System::Globalization::NumberStyles style);
public static decimal Parse (string s, System.Globalization.NumberStyles style);
static member Parse : string * System.Globalization.NumberStyles -> decimal
Public Shared Function Parse (s As String, style As NumberStyles) As Decimal

Parameters

s
String

The string representation of the number to convert.

style
NumberStyles

A bitwise combination of NumberStyles values that indicates the style elements that can be present in s. A typical value to specify is Number.

Returns

The Decimal number equivalent to the number contained in s as specified by style.

Exceptions

style is not a NumberStyles value.

-or-

style is the AllowHexSpecifier value.

s is not in the correct format.

s represents a number less than Decimal.MinValue or greater than Decimal.MaxValue

Examples

The following code example uses the Parse(String, NumberStyles) method to parse the string representations of Decimal values using the en-US culture.

string value;
decimal number;
NumberStyles style;

// Parse string with a floating point value using NumberStyles.None.
value = "8694.12";
style = NumberStyles.None;
try
{
   number = Decimal.Parse(value, style);
   Console.WriteLine("'{0}' converted to {1}.", value, number);
}
catch (FormatException)
{
   Console.WriteLine("Unable to parse '{0}'.", value);
}
// Displays:
//    Unable to parse '8694.12'.

// Parse string with a floating point value and allow decimal point.
style = NumberStyles.AllowDecimalPoint;
number = Decimal.Parse(value, style);
Console.WriteLine("'{0}' converted to {1}.", value, number);
// Displays:
//    '8694.12' converted to 8694.12.

// Parse string with negative value in parentheses
value = "(1,789.34)";
style = NumberStyles.AllowDecimalPoint | NumberStyles.AllowThousands |
        NumberStyles.AllowParentheses;
number = Decimal.Parse(value, style);
Console.WriteLine("'{0}' converted to {1}.", value, number);
// Displays:
//    '(1,789.34)' converted to -1789.34.

// Parse string using Number style
value = " -17,623.49 ";
style = NumberStyles.Number;
number = Decimal.Parse(value, style);
Console.WriteLine("'{0}' converted to {1}.", value, number);
// Displays:
//    ' -17,623.49 ' converted to -17623.49.
// Parse string with a floating point value using NumberStyles.None.
let value = "8694.12"
let style = NumberStyles.None
try
    let number = Decimal.Parse(value, style)
    printfn $"'{value}' converted to {number}."
with :? FormatException ->
    printfn $"Unable to parse '{value}'."
// Displays:
//    Unable to parse '8694.12'.

// Parse string with a floating point value and allow decimal point.
let style = NumberStyles.AllowDecimalPoint
let number = Decimal.Parse(value, style)
printfn $"'{value}' converted to {number}."
// Displays:
//    '8694.12' converted to 8694.12.

// Parse string with negative value in parentheses
let value = "(1,789.34)"
let style = 
    NumberStyles.AllowDecimalPoint ||| 
    NumberStyles.AllowThousands ||| 
    NumberStyles.AllowParentheses
let number = Decimal.Parse(value, style)
printfn $"'{value}' converted to {number}."
// Displays:
//    '(1,789.34)' converted to -1789.34.

// Parse string using Number style
let value = " -17,623.49 "
let style = NumberStyles.Number
let number = Decimal.Parse(value, style)
printfn $"'{value}' converted to {number}."
// Displays:
//    ' -17,623.49 ' converted to -17623.49.
Dim value As String
Dim number As Decimal
Dim style As NumberStyles

' Parse string with a floating point value using NumberStyles.None. 
value = "8694.12"
style = NumberStyles.None
Try
   number = Decimal.Parse(value, style)  
   Console.WriteLine("'{0}' converted to {1}.", value, number)
Catch e As FormatException
   Console.WriteLine("Unable to parse '{0}'.", value)
End Try
' Displays:
'    Unable to parse '8694.12'.

' Parse string with a floating point value and allow decimal point. 
style = NumberStyles.AllowDecimalPoint
number = Decimal.Parse(value, style)  
Console.WriteLine("'{0}' converted to {1}.", value, number)
' Displays:
'    '8694.12' converted to 8694.12.

' Parse string with negative value in parentheses
value = "(1,789.34)"
style = NumberStyles.AllowDecimalPoint Or NumberStyles.AllowThousands Or _
        NumberStyles.AllowParentheses 
number = Decimal.Parse(value, style)  
Console.WriteLine("'{0}' converted to {1}.", value, number)
' Displays:
'    '(1,789.34)' converted to -1789.34.

' Parse string using Number style
value = " -17,623.49 "
style = NumberStyles.Number
number = Decimal.Parse(value, style)  
Console.WriteLine("'{0}' converted to {1}.", value, number)
' Displays:
'    ' -17,623.49 ' converted to -17623.49.

Remarks

The style parameter defines the style elements (such as thousands separators, white space, and currency symbols) that are allowed in the s parameter for the parse operation to succeed. It must be a combination of bit flags from the NumberStyles enumeration. The following NumberStyles members are not supported:

Depending on the value of style, the s parameter may include the following elements:

[ws][$][sign][digits,]digits[.fractional-digits][e[sign]digits][ws]

Elements in square brackets ([ and ]) are optional. The following table describes each element.

Element Description
ws Optional white space. White space can appear at the beginning of s if style includes the NumberStyles.AllowLeadingWhite flag, and it can appear at the end of s if style includes the NumberStyles.AllowTrailingWhite flag.
$ A culture-specific currency symbol. Its position in the string is defined by the NumberFormatInfo.CurrencyNegativePattern and NumberFormatInfo.CurrencyPositivePattern properties of the current culture. The current culture's currency symbol can appear in s if style includes the NumberStyles.AllowCurrencySymbol flag.
sign An optional sign. The sign can appear at the beginning of s if style includes the NumberStyles.AllowLeadingSign flag, and it can appear at the end of s if style includes the NumberStyles.AllowTrailingSign flag. Parentheses can be used in s to indicate a negative value if style includes the NumberStyles.AllowParentheses flag.
digits A sequence of digits ranging from 0 to 9.
, A culture-specific thousands separator symbol. The current culture's thousands separator can appear in s if style includes the NumberStyles.AllowThousands flag.
. A culture-specific decimal point symbol. The current culture's decimal point symbol can appear in s if style includes the NumberStyles.AllowDecimalPoint flag.
fractional-digits A sequence of digits ranging from 0 to 9. Fractional digits can appear in s only if style includes the NumberStyles.AllowDecimalPoint flag.
e The 'e' or 'E' character, which indicates that the value is represented in exponential notation. The s parameter can represent a number in exponential notation if style includes the NumberStyles.AllowExponent flag.

Note

Any terminating NUL (U+0000) characters in s are ignored by the parsing operation, regardless of the value of the style argument.

A string with digits only (which corresponds to the None style) always parses successfully if it is in the range of the Decimal type. The remaining NumberStyles members control elements that may be but are not required to be present in the input string. The following table indicates how individual NumberStyles members affect the elements that may be present in s.

NumberStyles value Elements permitted in s in addition to digits
None The digits element only.
AllowDecimalPoint The . and fractional-digits elements.
AllowExponent The s parameter can also use exponential notation. This flag supports values in the form digitsEdigits; additional flags are needed to successfully parse strings with elements such as positive or negative signs and decimal point symbols.
AllowLeadingWhite The ws element at the beginning of s.
AllowTrailingWhite The ws element at the end of s.
AllowLeadingSign The sign element at the beginning of s.
AllowTrailingSign The sign element at the end of s.
AllowParentheses The sign element in the form of parentheses enclosing the numeric value.
AllowThousands The , element.
AllowCurrencySymbol The $ element.
Currency All. The s parameter cannot represent a hexadecimal number or a number in exponential notation.
Float The ws element at the beginning or end of s, sign at the beginning of s, and the . symbol. The s parameter can also use exponential notation.
Number The ws, sign, , and . elements.
Any All styles, except s cannot represent a hexadecimal number.

The s parameter is parsed using the formatting information in a NumberFormatInfo object initialized for the current system culture. For more information, see CurrentInfo.

A Decimal has 29 digits of precision. If s represents a number that has more than 29 digits, but has a fractional part and is within the range of MaxValue and MinValue, the number is rounded, not truncated, to 29 digits using rounding to nearest.

If a separator is encountered in the s parameter during a parse operation, styles includes the NumberStyles.AllowThousands and NumberStyles.AllowDecimalPoint values, and the applicable currency or number decimal and group separators are the same, the parse operation assumes that the separator is a decimal separator rather than a group separator. For more information about separators, see CurrencyDecimalSeparator, NumberDecimalSeparator, CurrencyGroupSeparator, and NumberGroupSeparator.

See also

Applies to

Parse(String, IFormatProvider)

Converts the string representation of a number to its Decimal equivalent using the specified culture-specific format information.

public:
 static System::Decimal Parse(System::String ^ s, IFormatProvider ^ provider);
public:
 static System::Decimal Parse(System::String ^ s, IFormatProvider ^ provider) = IParsable<System::Decimal>::Parse;
public static decimal Parse (string s, IFormatProvider provider);
public static decimal Parse (string s, IFormatProvider? provider);
static member Parse : string * IFormatProvider -> decimal
Public Shared Function Parse (s As String, provider As IFormatProvider) As Decimal

Parameters

s
String

The string representation of the number to convert.

provider
IFormatProvider

An IFormatProvider that supplies culture-specific parsing information about s.

Returns

The Decimal number equivalent to the number contained in s as specified by provider.

Implements

Exceptions

s is not of the correct format.

s represents a number less than Decimal.MinValue or greater than Decimal.MaxValue.

Examples

The following example is the button click event handler of a Web form. It uses the array returned by the HttpRequest.UserLanguages property to determine the user's locale. It then instantiates a CultureInfo object that corresponds to that locale. The NumberFormatInfo object that belongs to that CultureInfo object is then passed to the Parse(String, IFormatProvider) method to convert the user's input to a Decimal value.

protected void OkToDecimal_Click(object sender, EventArgs e)
{
    string locale;
    decimal number;
    CultureInfo culture;

    // Return if string is empty
    if (String.IsNullOrEmpty(this.inputNumber.Text))
        return;

    // Get locale of web request to determine possible format of number
    if (Request.UserLanguages.Length == 0)
        return;
    locale = Request.UserLanguages[0];
    if (String.IsNullOrEmpty(locale))
        return;

    // Instantiate CultureInfo object for the user's locale
    culture = new CultureInfo(locale);

    // Convert user input from a string to a number
    try
    {
        number = Decimal.Parse(this.inputNumber.Text, culture.NumberFormat);
    }
    catch (FormatException)
    {
        return;
    }
    catch (Exception)
    {
        return;
    }
    // Output number to label on web form
    this.outputNumber.Text = "Number is " + number.ToString();
}
Protected Sub OkToDecimal_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles OkToDecimal.Click
   Dim locale As String
   Dim culture As CultureInfo
   Dim number As Decimal

   ' Return if string is empty
   If String.IsNullOrEmpty(Me.inputNumber.Text) Then Exit Sub

   ' Get locale of web request to determine possible format of number
   If Request.UserLanguages.Length = 0 Then Exit Sub
   locale = Request.UserLanguages(0)
   If String.IsNullOrEmpty(locale) Then Exit Sub

   ' Instantiate CultureInfo object for the user's locale
   culture = New CultureInfo(locale)

   ' Convert user input from a string to a number
   Try
      number = Decimal.Parse(Me.inputNumber.Text, culture.NumberFormat)
   Catch ex As FormatException
      Exit Sub
   Catch ex As Exception
      Exit Sub
   End Try

   ' Output number to label on web form
   Me.outputNumber.Text = "Number is " & number.ToString()
End Sub

Remarks

This overload of the Parse(String, IFormatProvider) method is commonly used to convert text that can be formatted in a variety of ways to a Decimal value. For example, it can be used to convert the text entered by a user into an HTML text box to a numeric value.

The s parameter contains a number of the form:

[ws][sign][digits,]digits[.fractional-digits][ws]

Elements in square brackets ([ and ]) are optional. The following table describes each element.

Element Description
ws Optional white space.
sign An optional sign.
digits A sequence of digits ranging from 0 to 9.
, A culture-specific thousands separator symbol.
. A culture-specific decimal point symbol.
fractional-digits A sequence of digits ranging from 0 to 9.

The s parameter is interpreted using the NumberStyles.Number style. This means that white space and thousands separators are allowed but currency symbols are not. To explicitly define the elements (such as currency symbols, thousands separators, and white space) that can be present in s, use the Decimal.Parse(String, NumberStyles, IFormatProvider) method.

The provider parameter is an IFormatProvider implementation, such as a NumberFormatInfo or CultureInfo object. The provider parameter supplies culture-specific information used in parsing. If provider is null, the thread current culture is used.

A Decimal object has 29 digits of precision. If s represents a number that has more than 29 digits, but has a fractional part and is within the range of MaxValue and MinValue, the number is rounded, not truncated, to 29 digits using rounding to nearest.

If a separator is encountered in the s parameter during a parse operation, and the applicable currency or number decimal and group separators are the same, the parse operation assumes that the separator is a decimal separator rather than a group separator. For more information about separators, see CurrencyDecimalSeparator, NumberDecimalSeparator, CurrencyGroupSeparator, and NumberGroupSeparator.

See also

Applies to

Parse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider)

Parses a span of UTF-8 characters into a value.

public static decimal Parse (ReadOnlySpan<byte> utf8Text, System.Globalization.NumberStyles style = System.Globalization.NumberStyles.Number, IFormatProvider? provider = default);
static member Parse : ReadOnlySpan<byte> * System.Globalization.NumberStyles * IFormatProvider -> decimal
Public Shared Function Parse (utf8Text As ReadOnlySpan(Of Byte), Optional style As NumberStyles = System.Globalization.NumberStyles.Number, Optional provider As IFormatProvider = Nothing) As Decimal

Parameters

utf8Text
ReadOnlySpan<Byte>

The span of UTF-8 characters to parse.

style
NumberStyles

A bitwise combination of number styles that can be present in utf8Text.

provider
IFormatProvider

An object that provides culture-specific formatting information about utf8Text.

Returns

The result of parsing utf8Text.

Implements

Applies to

Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider)

Converts the span representation of a number to its Decimal equivalent using the specified style and culture-specific format.

public static decimal Parse (ReadOnlySpan<char> s, System.Globalization.NumberStyles style = System.Globalization.NumberStyles.Number, IFormatProvider? provider = default);
public static decimal Parse (ReadOnlySpan<char> s, System.Globalization.NumberStyles style = System.Globalization.NumberStyles.Number, IFormatProvider provider = default);
static member Parse : ReadOnlySpan<char> * System.Globalization.NumberStyles * IFormatProvider -> decimal
Public Shared Function Parse (s As ReadOnlySpan(Of Char), Optional style As NumberStyles = System.Globalization.NumberStyles.Number, Optional provider As IFormatProvider = Nothing) As Decimal

Parameters

s
ReadOnlySpan<Char>

The span containing the characters representing the number to convert.

style
NumberStyles

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
IFormatProvider

An IFormatProvider object that supplies culture-specific information about the format of s.

Returns

The Decimal number equivalent to the number contained in s as specified by style and provider.

Implements

Applies to

Parse(String, NumberStyles, IFormatProvider)

Converts the string representation of a number to its Decimal equivalent using the specified style and culture-specific format.

public:
 static System::Decimal Parse(System::String ^ s, System::Globalization::NumberStyles style, IFormatProvider ^ provider);
public:
 static System::Decimal Parse(System::String ^ s, System::Globalization::NumberStyles style, IFormatProvider ^ provider) = System::Numerics::INumberBase<System::Decimal>::Parse;
public static decimal Parse (string s, System.Globalization.NumberStyles style, IFormatProvider provider);
public static decimal Parse (string s, System.Globalization.NumberStyles style, IFormatProvider? provider);
static member Parse : string * System.Globalization.NumberStyles * IFormatProvider -> decimal
Public Shared Function Parse (s As String, style As NumberStyles, provider As IFormatProvider) As Decimal

Parameters

s
String

The string representation of the number to convert.

style
NumberStyles

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
IFormatProvider

An IFormatProvider object that supplies culture-specific information about the format of s.

Returns

The Decimal number equivalent to the number contained in s as specified by style and provider.

Implements

Exceptions

s is not in the correct format.

s represents a number less than Decimal.MinValue or greater than Decimal.MaxValue.

style is not a NumberStyles value.

-or-

style is the AllowHexSpecifier value.

Examples

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

string value;
decimal number;
NumberStyles style;
CultureInfo provider;

// Parse string using " " as the thousands separator
// and "," as the decimal separator for fr-FR culture.
value = "892 694,12";
style = NumberStyles.AllowDecimalPoint | NumberStyles.AllowThousands;
provider = new CultureInfo("fr-FR");

number = Decimal.Parse(value, style, provider);
Console.WriteLine("'{0}' converted to {1}.", value, number);
// Displays:
//    '892 694,12' converted to 892694.12.

try
{
   number = Decimal.Parse(value, style, CultureInfo.InvariantCulture);
   Console.WriteLine("'{0}' converted to {1}.", value, number);
}
catch (FormatException)
{
   Console.WriteLine("Unable to parse '{0}'.", value);
}
// Displays:
//    Unable to parse '892 694,12'.

// Parse string using "$" as the currency symbol for en-GB and
// en-US cultures.
value = "$6,032.51";
style = NumberStyles.Number | NumberStyles.AllowCurrencySymbol;
provider = new CultureInfo("en-GB");

try
{
   number = Decimal.Parse(value, style, provider);
   Console.WriteLine("'{0}' converted to {1}.", value, number);
}
catch (FormatException)
{
   Console.WriteLine("Unable to parse '{0}'.", value);
}
// Displays:
//    Unable to parse '$6,032.51'.

provider = new CultureInfo("en-US");
number = Decimal.Parse(value, style, provider);
Console.WriteLine("'{0}' converted to {1}.", value, number);
// Displays:
//    '$6,032.51' converted to 6032.51.
// Parse string using " " as the thousands separator
// and "," as the decimal separator for fr-FR culture.
let value = "892 694,12"
let style = NumberStyles.AllowDecimalPoint ||| NumberStyles.AllowThousands
let provider = CultureInfo "fr-FR"

let number = Decimal.Parse(value, style, provider)
printfn $"'{value}' converted to {number}."
// Displays:
//    '892 694,12' converted to 892694.12.

try
    let number = Decimal.Parse(value, style, CultureInfo.InvariantCulture)
    printfn $"'{value}' converted to {number}."
with :? FormatException ->
    printfn $"Unable to parse '{value}'."
// Displays:
//    Unable to parse '892 694,12'.

// Parse string using "$" as the currency symbol for en-GB and
// en-US cultures.
let value = "$6,032.51"
let style = NumberStyles.Number ||| NumberStyles.AllowCurrencySymbol
let provider = CultureInfo "en-GB"

try
    let number = Decimal.Parse(value, style, provider)
    printfn $"'{value}' converted to {number}."
with :? FormatException ->
    printfn $"Unable to parse '{value}'."
// Displays:
//    Unable to parse '$6,032.51'.

let provider = CultureInfo "en-US"
let number = Decimal.Parse(value, style, provider)
printfn $"'{value}' converted to {number}."
// Displays:
//    '$6,032.51' converted to 6032.51.
Dim value As String
Dim number As Decimal
Dim style As NumberStyles
Dim provider As CultureInfo

' Parse string using " " as the thousands separator 
' and "," as the decimal separator for fr-FR culture.
value = "892 694,12"
style = NumberStyles.AllowDecimalPoint Or NumberStyles.AllowThousands
provider = New CultureInfo("fr-FR")

number = Decimal.Parse(value, style, provider)  
Console.WriteLine("'{0}' converted to {1}.", value, number)
' Displays: 
'    '892 694,12' converted to 892694.12.

Try
   number = Decimal.Parse(value, style, CultureInfo.InvariantCulture)  
   Console.WriteLine("'{0}' converted to {1}.", value, number)
Catch e As FormatException
   Console.WriteLine("Unable to parse '{0}'.", value)
End Try
' Displays: 
'    Unable to parse '892 694,12'.  

' Parse string using "$" as the currency symbol for en-GB and
' en-US cultures.
value = "$6,032.51"
style = NumberStyles.Number Or NumberStyles.AllowCurrencySymbol
provider = New CultureInfo("en-GB")

Try
   number = Decimal.Parse(value, style, provider)  
   Console.WriteLine("'{0}' converted to {1}.", value, number)
Catch e As FormatException
   Console.WriteLine("Unable to parse '{0}'.", value)
End Try
' Displays: 
'    Unable to parse '$6,032.51'.

provider = New CultureInfo("en-US")
number = Decimal.Parse(value, style, provider)  
Console.WriteLine("'{0}' converted to {1}.", value, number)
' Displays: 
'    '$6,032.51' converted to 6032.51.

Remarks

The style parameter defines the allowable format of the s parameter for the parse operation to succeed. It must be a combination of bit flags from the NumberStyles enumeration. The following NumberStyles members are not supported:

Depending on the value of style, the s parameter may include the following elements:

[ws][$][sign][digits,]digits[.fractional-digits][e[sign]digits][ws]

Elements in square brackets ([ and ]) are optional. The following table describes each element.

Element Description
$ A culture-specific currency symbol. Its position in the string is defined by the CurrencyNegativePattern and CurrencyPositivePattern properties of the NumberFormatInfo object returned by the GetFormat method of the provider parameter. The currency symbol can appear in s if style includes the NumberStyles.AllowCurrencySymbol flag.
ws Optional white space. White space can appear at the beginning of s if style includes the NumberStyles.AllowLeadingWhite flag, and it can appear at the end of s if style includes the NumberStyles.AllowTrailingWhite flag.
sign An optional sign. The sign can appear at the beginning of s if style includes the NumberStyles.AllowLeadingSign flag, and it can appear at the end of s if style includes the NumberStyles.AllowTrailingSign flag. Parentheses can be used in s to indicate a negative value if style includes the NumberStyles.AllowParentheses flag.
digits A sequence of digits ranging from 0 to 9.
, A culture-specific thousands separator symbol. The thousands separator of the culture defined by provider can appear in s if style includes the NumberStyles.AllowThousands flag.
. A culture-specific decimal point symbol. The decimal point symbol of the culture defined by provider can appear in s if style includes the NumberStyles.AllowDecimalPoint flag.
fractional-digits A sequence of digits ranging from 0 to 9. Fractional digits can appear in s only if style includes the NumberStyles.AllowDecimalPoint flag.
e The 'e' or 'E' character, which indicates that the value is represented in exponential notation. The s parameter can represent a number in exponential notation if style includes the NumberStyles.AllowExponent flag.

Note

Any terminating NUL (U+0000) characters in s are ignored by the parsing operation, regardless of the value of the style argument.

A string with digits only (which corresponds to the None style) always parses successfully if it is in the range of the Decimal type. The remaining NumberStyles members control elements that may be but are not required to be present in the input string. The following table indicates how individual NumberStyles members affect the elements that may be present in s.

NumberStyles value Elements permitted in s in addition to digits
None The digits element only.
AllowDecimalPoint The . and fractional-digits elements.
AllowExponent The s parameter can also use exponential notation. This flag supports values in the form digitsEdigits; additional flags are needed to successfully parse strings with elements such as positive or negative signs and decimal point symbols.
AllowLeadingWhite The ws element at the beginning of s.
AllowTrailingWhite The ws element at the end of s.
AllowLeadingSign The sign element at the beginning of s.
AllowTrailingSign The sign element at the end of s.
AllowParentheses The sign element in the form of parentheses enclosing the numeric value.
AllowThousands The , element.
AllowCurrencySymbol The $ element.
Currency All. The s parameter cannot represent a hexadecimal number or a number in exponential notation.
Float The ws element at the beginning or end of s, sign at the beginning of s, and the . symbol. The s parameter can also use exponential notation.
Number The ws, sign, ,, and . elements.
Any All styles, except s cannot represent a hexadecimal number.

The provider parameter is an IFormatProvider implementation, such as a NumberFormatInfo or CultureInfo object. The provider parameter supplies culture-specific information used in parsing. If provider is null, the thread current culture is used.

A Decimal object has 29 digits of precision. If s represents a number that has more than 29 digits, but has a fractional part and is within the range of MaxValue and MinValue, the number is rounded, not truncated, to 29 digits using rounding to nearest.

If a separator is encountered in the s parameter during a parse operation, and the applicable currency or number decimal and group separators are the same, the parse operation assumes that the separator is a decimal separator rather than a group separator. For more information about separators, see CurrencyDecimalSeparator, NumberDecimalSeparator, CurrencyGroupSeparator, and NumberGroupSeparator.

See also

Applies to