Decimal.Parse 方法 
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
将数字的字符串表示形式转换为其等效 Decimal。
重载
| Parse(String) | 将数字的字符串表示形式转换为其等效 Decimal。 | 
| Parse(ReadOnlySpan<Byte>, IFormatProvider) | 将 UTF-8 字符的范围分析为值。 | 
| Parse(ReadOnlySpan<Char>, IFormatProvider) | 将字符的范围分析为值。 | 
| Parse(String, NumberStyles) | 将指定样式中的数字的字符串表示形式转换为其等效 Decimal。 | 
| Parse(String, IFormatProvider) | 使用指定的区域性特定格式信息将数字的字符串表示形式转换为其等效 Decimal。 | 
| Parse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider) | 将 UTF-8 字符的范围分析为值。 | 
| Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider) | 使用指定的样式和区域性特定格式将数字的跨度表示形式转换为其等效 Decimal。 | 
| Parse(String, NumberStyles, IFormatProvider) | 使用指定的样式和区域性特定格式将数字的字符串表示形式转换为其等效 Decimal。 | 
Parse(String)
- Source:
- Decimal.cs
- Source:
- Decimal.cs
- Source:
- Decimal.cs
将数字的字符串表示形式转换为其等效 Decimal。
public:
 static System::Decimal Parse(System::String ^ s);public static decimal Parse (string s);static member Parse : string -> decimalPublic Shared Function Parse (s As String) As Decimal参数
- s
- String
要转换的数字的字符串表示形式。
返回
等效于 s中包含的数字。
例外
              s
              null。
              s 格式不正确。
              s 表示小于 Decimal.MinValue 或大于 Decimal.MaxValue的数字。
示例
下面的代码示例使用 Parse(String) 方法分析 Decimal 值的字符串表示形式。
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'.
注解
参数 s 包含多种形式:
[ws][sign][digits,]digits[.fractional-digits][ws]
方括号 ([ 和 ]) 中的元素是可选的。 下表描述了每个元素。
| 元素 | 描述 | 
|---|---|
| ws | 可选空格。 | 
| 签名 | 可选符号。 | 
| 位 | 一系列数字,范围从 0 到 9。 | 
| 、 | 区域性特定的千位分隔符符号。 | 
| 。 | 区域性特定的小数点符号。 | 
| 小数位数 | 一系列数字,范围从 0 到 9。 | 
使用 NumberStyles.Number 样式解释参数 s。 这意味着允许空格和数千个分隔符,但货币符号不是。 若要显式定义可以存在于 s中的元素(如货币符号、千位分隔符和空格),请使用 Decimal.Parse(String, NumberStyles) 或 Decimal.Parse(String, NumberStyles, IFormatProvider) 方法。
使用为当前系统区域性初始化的 NumberFormatInfo 中的格式信息分析参数 s。 有关详细信息,请参阅 CurrentInfo。 若要使用其他区域性的格式设置信息分析字符串,请使用 Decimal.Parse(String, IFormatProvider) 或 Decimal.Parse(String, NumberStyles, IFormatProvider) 方法。
如有必要,使用舍入到最接近的舍入将 s 的值舍入。
              Decimal 的精度为 29 位。 如果 s 表示数字超过 29 位,但具有小数部分,并且位于 MaxValue 和 MinValue范围内,则数字将舍入(而不是截断)到 29 位(使用舍入到最接近)。
如果在分析操作期间遇到 s 参数中的分隔符,并且适用的货币或数字小数和组分隔符相同,则分析操作假定分隔符是小数分隔符,而不是组分隔符。 有关分隔符的详细信息,请参阅 CurrencyDecimalSeparator、NumberDecimalSeparator、CurrencyGroupSeparator和 NumberGroupSeparator。
另请参阅
- TryParse
- ToString()
- 分析 .NET 中的数值字符串
适用于
Parse(ReadOnlySpan<Byte>, IFormatProvider)
- Source:
- Decimal.cs
- Source:
- Decimal.cs
将 UTF-8 字符的范围分析为值。
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 -> decimalPublic Shared Function Parse (utf8Text As ReadOnlySpan(Of Byte), provider As IFormatProvider) As Decimal参数
- utf8Text
- ReadOnlySpan<Byte>
要分析的 UTF-8 字符的范围。
- provider
- IFormatProvider
一个对象,提供有关 utf8Text的区域性特定格式设置信息。
返回
分析 utf8Text的结果。
实现
适用于
Parse(ReadOnlySpan<Char>, IFormatProvider)
- Source:
- Decimal.cs
- Source:
- Decimal.cs
- Source:
- Decimal.cs
将字符的范围分析为值。
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 -> decimalPublic Shared Function Parse (s As ReadOnlySpan(Of Char), provider As IFormatProvider) As Decimal参数
- s
- ReadOnlySpan<Char>
要分析的字符范围。
- provider
- IFormatProvider
一个对象,提供有关 s的区域性特定格式设置信息。
返回
分析 s的结果。
实现
适用于
Parse(String, NumberStyles)
- Source:
- Decimal.cs
- Source:
- Decimal.cs
- Source:
- Decimal.cs
将指定样式中的数字的字符串表示形式转换为其等效 Decimal。
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 -> decimalPublic Shared Function Parse (s As String, style As NumberStyles) As Decimal参数
- s
- String
要转换的数字的字符串表示形式。
- style
- NumberStyles
              NumberStyles 值的按位组合,指示 s中可以存在的样式元素。 要指定的典型值为 Number。
返回
与由 style指定的 s 中包含的数字等效的 Decimal 数。
例外
              s
              null。
              s 格式不正确。
              s 表示小于 Decimal.MinValue 或大于 Decimal.MaxValue 的数字
示例
下面的代码示例使用 Parse(String, NumberStyles) 方法使用 en-US 区域性分析 Decimal 值的字符串表示形式。
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.
注解
              style 参数定义 s 参数中允许的样式元素(如千位分隔符、空格和货币符号),以便分析操作成功。 它必须是 NumberStyles 枚举中的位标志的组合。 不支持以下 NumberStyles 成员:
根据 style的值,s 参数可能包括以下元素:
[ws][$][sign][digits,]digits[.fractional-digits][e[sign]digits][ws]
方括号 ([ 和 ]) 中的元素是可选的。 下表描述了每个元素。
| 元素 | 描述 | 
|---|---|
| ws | 可选空格。 如果 style包含 NumberStyles.AllowLeadingWhite 标志,则空格可能出现在s的开头,如果style包含 NumberStyles.AllowTrailingWhite 标志,则它将显示在s末尾。 | 
| $ | 区域性特定的货币符号。 字符串中的位置由当前区域性的 NumberFormatInfo.CurrencyNegativePattern 和 NumberFormatInfo.CurrencyPositivePattern 属性定义。 如果 style包含 NumberStyles.AllowCurrencySymbol 标志,则当前区域性的货币符号可以出现在s中。 | 
| 签名 | 可选符号。 如果 style包含 NumberStyles.AllowLeadingSign 标志,则符号可以在s开头显示,如果style包含 NumberStyles.AllowTrailingSign 标志,则它将显示在s末尾。 如果style包含 NumberStyles.AllowParentheses 标志,则可以在s中使用括号来指示负值。 | 
| 位 | 一系列数字,范围从 0 到 9。 | 
| 、 | 区域性特定的千位分隔符符号。 如果 style包含 NumberStyles.AllowThousands 标志,则当前区域性的千位分隔符可以出现在s中。 | 
| 。 | 区域性特定的小数点符号。 如果 style包含 NumberStyles.AllowDecimalPoint 标志,则当前区域性的小数点符号可以出现在s中。 | 
| 小数位数 | 一系列数字,范围从 0 到 9。 仅当 style包含 NumberStyles.AllowDecimalPoint 标志时,小数位数才会显示在s中。 | 
| e | “e”或“E”字符,指示该值以指数表示法表示。 如果 style包含 NumberStyles.AllowExponent 标志,s参数可以表示指数表示法中的数字。 | 
注意
无论 style 参数的值如何,分析操作都会忽略 s 中任何终止的 NUL (U+0000) 字符。
仅包含数字(对应于 None 样式)的字符串在 Decimal 类型范围内时始终成功分析。 其余 NumberStyles 成员控制可能但不需要存在于输入字符串中的元素。 下表指示单个 NumberStyles 成员如何影响 s中可能存在的元素。
| NumberStyles 值 | 除数字外允许的元素 | 
|---|---|
| None | 仅 元素 | 
| AllowDecimalPoint | 。 和 小数位数 元素。 | 
| AllowExponent | s参数也可以使用指数表示法。 此标志支持 数字形式的值E位;需要其他标志才能成功分析包含正号或负号和小数点符号等元素的字符串。 | 
| AllowLeadingWhite | s开头的 ws 元素。 | 
| AllowTrailingWhite | s末尾的 ws 元素。 | 
| AllowLeadingSign | s开头的 符号 元素。 | 
| AllowTrailingSign | s末尾的 符号 元素。 | 
| AllowParentheses | 符号 元素,以括住数值的括号形式。 | 
| AllowThousands | , 元素。 | 
| AllowCurrencySymbol | $ 元素。 | 
| Currency | 都。 s参数不能表示十六进制数或指数表示法中的数字。 | 
| Float | ws 元素位于 s的开头或末尾,符号s开头,以及.符号。s参数也可以使用指数表示法。 | 
| Number | ws、sign、,和.元素。 | 
| Any | 除 s之外的所有样式都不能表示十六进制数。 | 
使用为当前系统区域性初始化的 NumberFormatInfo 对象中的格式信息分析 s 参数。 有关详细信息,请参阅 CurrentInfo。
              Decimal 的精度为 29 位。 如果 s 表示数字超过 29 位,但具有小数部分,并且位于 MaxValue 和 MinValue范围内,则数字将舍入(而不是截断)到 29 位(使用舍入到最接近)。
如果在分析操作期间 s 参数中遇到分隔符,styles 包括 NumberStyles.AllowThousands 和 NumberStyles.AllowDecimalPoint 值,并且适用的货币或数字小数和组分隔符相同,则分析操作假定分隔符是小数分隔符,而不是组分隔符。 有关分隔符的详细信息,请参阅 CurrencyDecimalSeparator、NumberDecimalSeparator、CurrencyGroupSeparator和 NumberGroupSeparator。
另请参阅
- TryParse
- ToString()
- 分析 .NET 中的数值字符串
适用于
Parse(String, IFormatProvider)
- Source:
- Decimal.cs
- Source:
- Decimal.cs
- Source:
- Decimal.cs
使用指定的区域性特定格式信息将数字的字符串表示形式转换为其等效 Decimal。
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 -> decimalPublic Shared Function Parse (s As String, provider As IFormatProvider) As Decimal参数
- s
- String
要转换的数字的字符串表示形式。
- provider
- IFormatProvider
提供有关 s的区域性特定分析信息的 IFormatProvider。
返回
与由 provider指定的 s 中包含的数字等效的 Decimal 数。
实现
例外
              s
              null。
              s 的格式不正确。
              s 表示小于 Decimal.MinValue 或大于 Decimal.MaxValue的数字。
示例
以下示例是 Web 窗体的按钮单击事件处理程序。 它使用 HttpRequest.UserLanguages 属性返回的数组来确定用户的区域设置。 然后,它会实例化与该区域设置对应的 CultureInfo 对象。 然后,将属于该 CultureInfo 对象的 NumberFormatInfo 对象传递给 Parse(String, IFormatProvider) 方法,将用户的输入转换为 Decimal 值。
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
注解
Parse(String, IFormatProvider) 方法的这种重载通常用于将可通过多种方式格式化的文本转换为 Decimal 值。 例如,它可用于将用户输入的文本转换为 HTML 文本框,并将其转换为数值。
              s 参数包含多种形式:
[ws][sign][digits,]digits[.fractional-digits][ws]
方括号 ([ 和 ]) 中的元素是可选的。 下表描述了每个元素。
| 元素 | 描述 | 
|---|---|
| ws | 可选空格。 | 
| 签名 | 可选符号。 | 
| 位 | 一系列数字,范围从 0 到 9。 | 
| 、 | 区域性特定的千位分隔符符号。 | 
| 。 | 区域性特定的小数点符号。 | 
| 小数位数 | 一系列数字,范围从 0 到 9。 | 
              s 参数使用 NumberStyles.Number 样式进行解释。 这意味着允许空格和数千个分隔符,但货币符号不是。 若要显式定义可以存在于 s中的元素(如货币符号、千位分隔符和空格),请使用 Decimal.Parse(String, NumberStyles, IFormatProvider) 方法。
              provider 参数是 IFormatProvider 实现,例如 NumberFormatInfo 或 CultureInfo 对象。 
              provider 参数提供分析中使用的区域性特定信息。 如果 providernull,则使用线程当前区域性。
              Decimal 对象具有 29 位精度。 如果 s 表示数字超过 29 位,但具有小数部分,并且位于 MaxValue 和 MinValue范围内,则数字将舍入(而不是截断)到 29 位(使用舍入到最接近)。
如果在分析操作期间 s 参数中遇到分隔符,并且适用的货币或数字小数和组分隔符相同,则分析操作假定分隔符是小数分隔符,而不是组分隔符。 有关分隔符的详细信息,请参阅 CurrencyDecimalSeparator、NumberDecimalSeparator、CurrencyGroupSeparator和 NumberGroupSeparator。
另请参阅
- TryParse
- ToString()
- 分析 .NET 中的数值字符串
适用于
Parse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider)
- Source:
- Decimal.cs
- Source:
- Decimal.cs
将 UTF-8 字符的范围分析为值。
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 -> decimalPublic Shared Function Parse (utf8Text As ReadOnlySpan(Of Byte), Optional style As NumberStyles = System.Globalization.NumberStyles.Number, Optional provider As IFormatProvider = Nothing) As Decimal参数
- utf8Text
- ReadOnlySpan<Byte>
要分析的 UTF-8 字符的范围。
- style
- NumberStyles
              utf8Text中可以存在的数字样式的按位组合。
- provider
- IFormatProvider
一个对象,提供有关 utf8Text的区域性特定格式设置信息。
返回
分析 utf8Text的结果。
实现
适用于
Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider)
- Source:
- Decimal.cs
- Source:
- Decimal.cs
- Source:
- Decimal.cs
使用指定的样式和区域性特定格式将数字的跨度表示形式转换为其等效 Decimal。
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 -> decimalPublic Shared Function Parse (s As ReadOnlySpan(Of Char), Optional style As NumberStyles = System.Globalization.NumberStyles.Number, Optional provider As IFormatProvider = Nothing) As Decimal参数
- s
- ReadOnlySpan<Char>
包含表示要转换的数字的字符的范围。
- style
- NumberStyles
              NumberStyles 值的按位组合,指示 s中可以存在的样式元素。 要指定的典型值为 Number。
- provider
- IFormatProvider
一个 IFormatProvider 对象,该对象提供有关 s格式的区域性特定信息。
返回
与由 style 和 provider指定的 s 中包含的数字等效的 Decimal 数。
实现
适用于
Parse(String, NumberStyles, IFormatProvider)
- Source:
- Decimal.cs
- Source:
- Decimal.cs
- Source:
- Decimal.cs
使用指定的样式和区域性特定格式将数字的字符串表示形式转换为其等效 Decimal。
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 -> decimalPublic Shared Function Parse (s As String, style As NumberStyles, provider As IFormatProvider) As Decimal参数
- s
- String
要转换的数字的字符串表示形式。
- style
- NumberStyles
              NumberStyles 值的按位组合,指示 s中可以存在的样式元素。 要指定的典型值为 Number。
- provider
- IFormatProvider
一个 IFormatProvider 对象,该对象提供有关 s格式的区域性特定信息。
返回
与由 style 和 provider指定的 s 中包含的数字等效的 Decimal 数。
实现
例外
              s 格式不正确。
              s 表示小于 Decimal.MinValue 或大于 Decimal.MaxValue的数字。
              s
              null。
示例
以下示例使用各种 style 和 provider 参数分析 Decimal 值的字符串表示形式。
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.
注解
              style 参数定义 s 参数允许的格式,以便分析操作成功。 它必须是 NumberStyles 枚举中的位标志的组合。 不支持以下 NumberStyles 成员:
根据 style的值,s 参数可能包括以下元素:
[ws][$][sign][digits,]digits[.fractional-digits][e[sign]digits][ws]
方括号 ([ 和 ]) 中的元素是可选的。 下表描述了每个元素。
| 元素 | 描述 | 
|---|---|
| $ | 区域性特定的货币符号。 字符串中的位置由 provider参数 GetFormat 方法返回的 NumberFormatInfo 对象的 CurrencyNegativePattern 和 CurrencyPositivePattern 属性定义。 如果style包含 NumberStyles.AllowCurrencySymbol 标志,货币符号可以出现在s中。 | 
| ws | 可选空格。 如果 style包含 NumberStyles.AllowLeadingWhite 标志,则空格可能出现在s的开头,如果style包含 NumberStyles.AllowTrailingWhite 标志,则它将显示在s末尾。 | 
| 签名 | 可选符号。 如果 style包含 NumberStyles.AllowLeadingSign 标志,则符号可以在s开头显示,如果style包含 NumberStyles.AllowTrailingSign 标志,则它将显示在s末尾。 如果style包含 NumberStyles.AllowParentheses 标志,则可以在s中使用括号来指示负值。 | 
| 位 | 一系列数字,范围从 0 到 9。 | 
| 、 | 区域性特定的千位分隔符符号。 如果 style包含 NumberStyles.AllowThousands 标志,则由provider定义的区域性的千位分隔符可以出现在s中。 | 
| 。 | 区域性特定的小数点符号。 如果 style包含 NumberStyles.AllowDecimalPoint 标志,则由provider定义的区域性的小数点符号可以出现在s中。 | 
| 小数位数 | 一系列数字,范围从 0 到 9。 仅当 style包含 NumberStyles.AllowDecimalPoint 标志时,小数位数才会显示在s中。 | 
| e | “e”或“E”字符,指示该值以指数表示法表示。 如果 style包含 NumberStyles.AllowExponent 标志,s参数可以表示指数表示法中的数字。 | 
注意
无论 style 参数的值如何,分析操作都会忽略 s 中任何终止的 NUL (U+0000) 字符。
仅包含数字(对应于 None 样式)的字符串在 Decimal 类型范围内时始终成功分析。 其余 NumberStyles 成员控制可能但不需要存在于输入字符串中的元素。 下表指示单个 NumberStyles 成员如何影响 s中可能存在的元素。
| NumberStyles 值 | 除数字外允许的元素 | 
|---|---|
| None | 仅 元素 | 
| AllowDecimalPoint | 。 和 小数位数 元素。 | 
| AllowExponent | s参数也可以使用指数表示法。  此标志支持 数字形式的值E位;需要其他标志才能成功分析包含正号或负号和小数点符号等元素的字符串。 | 
| AllowLeadingWhite | s开头的 ws 元素。 | 
| AllowTrailingWhite | s末尾的 ws 元素。 | 
| AllowLeadingSign | s开头的 符号 元素。 | 
| AllowTrailingSign | s末尾的 符号 元素。 | 
| AllowParentheses | 符号 元素,以括住数值的括号形式。 | 
| AllowThousands | , 元素。 | 
| AllowCurrencySymbol | $ 元素。 | 
| Currency | 都。 s参数不能表示十六进制数或指数表示法中的数字。 | 
| Float | ws 元素位于 s的开头或末尾,s开头 符号,以及 。 符号。s参数也可以使用指数表示法。 | 
| Number | ws、sign、、和 。 元素。 | 
| Any | 除 s之外的所有样式都不能表示十六进制数。 | 
              provider 参数是 IFormatProvider 实现,例如 NumberFormatInfo 或 CultureInfo 对象。 
              provider 参数提供分析中使用的区域性特定信息。 如果 providernull,则使用线程当前区域性。
              Decimal 对象具有 29 位精度。 如果 s 表示数字超过 29 位,但具有小数部分,并且位于 MaxValue 和 MinValue范围内,则数字将舍入(而不是截断)到 29 位(使用舍入到最接近)。
如果在分析操作期间 s 参数中遇到分隔符,并且适用的货币或数字小数和组分隔符相同,则分析操作假定分隔符是小数分隔符,而不是组分隔符。 有关分隔符的详细信息,请参阅 CurrencyDecimalSeparator、NumberDecimalSeparator、CurrencyGroupSeparator和 NumberGroupSeparator。
另请参阅
- TryParse
- ToString()
- 分析 .NET 中的数值字符串