Sdílet prostřednictvím


Decimal.Parse Method (String, NumberStyles)

Microsoft Silverlight will reach end of support after October 2021. Learn more.

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

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

Syntax

'Declaration
Public Shared Function Parse ( _
    s As String, _
    style As NumberStyles _
) As Decimal
public static decimal Parse(
    string s,
    NumberStyles style
)

Parameters

  • s
    Type: System.String
    The string representation of the number to convert.

Return Value

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

Exceptions

Exception Condition
ArgumentNullException

s is nulla null reference (Nothing in Visual Basic).

ArgumentException

style is not a NumberStyles value.

-or-

style is the AllowHexSpecifier value.

FormatException

s is not in the correct format.

OverflowException

s represents a number less than MinValue or greater than MaxValue

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.

A string with digits only (which corresponds to the None style) always parses successfully. 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.

Examples

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

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)
   outputBlock.Text &= String.Format("'{0}' converted to {1}.", value, number) & vbCrLf
Catch e As FormatException
   outputBlock.Text &= String.Format("Unable to parse '{0}'.", value) & vbCrLf
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)
outputBlock.Text &= String.Format("'{0}' converted to {1}.", value, number) & vbCrLf
' 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)
outputBlock.Text &= String.Format("'{0}' converted to {1}.", value, number) & vbCrLf
' 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)
outputBlock.Text &= String.Format("'{0}' converted to {1}.", value, number) & vbCrLf
' Displays:
'    ' -17,623.49 ' converted to -17623.49.
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);
   outputBlock.Text += String.Format("'{0}' converted to {1}.", value, number) + "\n";
}
catch (FormatException)
{
   outputBlock.Text += String.Format("Unable to parse '{0}'.", value) + "\n";
}
// 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);
outputBlock.Text += String.Format("'{0}' converted to {1}.", value, number) + "\n";
// 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);
outputBlock.Text += String.Format("'{0}' converted to {1}.", value, number) + "\n";
// 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);
outputBlock.Text += String.Format("'{0}' converted to {1}.", value, number) + "\n";
// Displays:
//    ' -17,623.49 ' converted to -17623.49.

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.