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 -> decimal
Public 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]
方括号 ([ and ]) 中的元素是可选的。 下表对每个元素进行了描述。
元素 | 说明 |
---|---|
ws | 可选空格。 |
sign | 可选符号。 |
位数 | 从 0 到 9 的数字序列。 |
, | 区域性特定的千位分隔符。 |
. | 区域性特定的小数点符号。 |
fractional-digits | 从 0 到 9 的数字序列。 |
参数 s
是使用 样式解释的 NumberStyles.Number 。 这意味着允许使用空格和数千个分隔符,但不允许使用货币符号。 若要显式定义 (元素,如货币符号、千位分隔符和空白) 中可能存在 s
,请使用 Decimal.Parse(String, NumberStyles) 或 Decimal.Parse(String, NumberStyles, IFormatProvider) 方法。
参数 s
是使用针对当前系统区域性初始化的 中的 NumberFormatInfo 格式设置信息进行分析的。 有关详细信息,请参阅 CurrentInfo。 若要使用其他区域性的格式设置信息分析字符串,请使用 Decimal.Parse(String, IFormatProvider) 或 Decimal.Parse(String, NumberStyles, IFormatProvider) 方法。
如有必要,使用舍入到最接近值对 的值 s
进行舍入。
的 Decimal 精度为 29 位。 如果 s
表示的数字超过 29 位,但具有小数部分,并且位于 和 MinValue的范围内MaxValue,则使用舍入到最接近的数字将数字舍入而不是截断为 29 位数字。
如果在分析操作期间在 参数中 s
遇到分隔符,并且适用的货币或数字十进制分隔符和组分隔符相同,则分析操作假定该分隔符是小数分隔符而不是组分隔符。 有关分隔符的详细信息,请参阅 CurrencyDecimalSeparator、 NumberDecimalSeparator、 CurrencyGroupSeparator和 NumberGroupSeparator。
另请参阅
适用于
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 -> decimal
Public 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 -> decimal
Public 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 -> decimal
Public Shared Function Parse (s As String, style As NumberStyles) As Decimal
参数
- s
- String
要转换的数字的字符串表示形式。
- style
- NumberStyles
NumberStyles 值的按位组合,指示可出现在 s
中的样式元素。 要指定的一个典型值为 Number。
返回
Decimal 数,它与由 s
所指定的 style
中包含的数字等效。
例外
s
为 null
。
s
的格式不正确。
示例
下面的代码示例使用 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]
方括号 ([ and ]) 中的元素是可选的。 下表对每个元素进行了描述。
元素 | 说明 |
---|---|
ws | 可选空格。 如果包含 标志,s 则开头会出现空格;如果style style 包含 NumberStyles.AllowTrailingWhite 标志,则它可能出现在 末尾s 。NumberStyles.AllowLeadingWhite |
$ | 区域性特定的货币符号。 它在字符串中的位置由 NumberFormatInfo.CurrencyNegativePattern 当前区域性的 和 NumberFormatInfo.CurrencyPositivePattern 属性定义。 如果style 包含 NumberStyles.AllowCurrencySymbol 标志,则当前区域性的货币符号可以出现在 中s 。 |
sign | 可选符号。 如果包含 标志,则符号可以出现在 的s 开头,如果style 包含 NumberStyles.AllowTrailingSign 标志,则它可显示在 的末尾s 。NumberStyles.AllowLeadingSignstyle 如果style 包含 标志,NumberStyles.AllowParentheses则可以在 中使用s 括号来指示负值。 |
位数 | 从 0 到 9 的数字序列。 |
, | 区域性特定的千位分隔符。 如果style 包含 NumberStyles.AllowThousands 标志,则当前区域性的千位分隔符可以出现在 中s 。 |
. | 区域性特定的小数点符号。 如果style 包含 NumberStyles.AllowDecimalPoint 标志,则当前区域性的小数点符号可以出现在 中s 。 |
fractional-digits | 从 0 到 9 的数字序列。 仅当包含 标志时style ,NumberStyles.AllowDecimalPoint小数位数才能显示在 中s 。 |
e | “e”或“E”字符,指示该值以指数表示法表示。 如果style 包含 标志,则 s 参数可以表示指数表示法中的NumberStyles.AllowExponent数字。 |
注意
分析操作将忽略中 s
任何 (U+0000) 字符的终止 NUL,而不考虑 参数的值 style
。
仅包含数字的字符串 (对应于 None 样式) 如果位于类型范围内 Decimal ,则始终会成功分析。 其余 NumberStyles 成员控制可能为 但不需要存在于输入字符串中的元素。 下表指示各个 NumberStyles 成员如何影响 中可能存在 s
的元素。
NumberStyles 值 | 除了数字外,还允许 在 中 |
---|---|
None | 仅限 digits 元素。 |
AllowDecimalPoint | 和小数位数元素。 |
AllowExponent | 参数 s 还可以使用指数表示法。 此标志支持 数字E数字形式的值;若要成功分析包含正号或负号和小数点符号等元素的字符串,还需要其他标志。 |
AllowLeadingWhite | 开头的 s ws 元素。 |
AllowTrailingWhite | 位于 末尾的 s ws 元素。 |
AllowLeadingSign | 开头的s 符号元素。 |
AllowTrailingSign | 末尾的s 符号元素。 |
AllowParentheses | 用括号括住数值的 符号 元素。 |
AllowThousands | 、 元素。 |
AllowCurrencySymbol | $ 元素。 |
Currency | 全部。 参数 s 不能表示十六进制数或指数表示法中的数字。 |
Float | 位于 开头或末尾的 s ws元素,符号位于 的s 开头,符号. 。 参数 s 还可以使用指数表示法。 |
Number |
ws 、 sign , 和 . 元素。 |
Any | 所有样式(除外 s )都不能表示十六进制数。 |
参数 s
是使用针对当前系统区域性初始化的 对象中的 NumberFormatInfo 格式设置信息进行分析的。 有关详细信息,请参阅 CurrentInfo。
的 Decimal 精度为 29 位。 如果 s
表示的数字超过 29 位,但具有小数部分,并且位于 和 MinValue的范围内MaxValue,则使用舍入到最接近的数字将数字舍入而不是截断为 29 位数字。
如果在分析操作期间在 参数中 s
遇到分隔符, styles
包括 NumberStyles.AllowThousands 和 NumberStyles.AllowDecimalPoint 值,并且适用的货币或数字十进制分隔符和组分隔符相同,则分析操作假定分隔符是小数分隔符,而不是组分隔符。 有关分隔符的详细信息,请参阅 CurrencyDecimalSeparator、 NumberDecimalSeparator、 CurrencyGroupSeparator和 NumberGroupSeparator。
另请参阅
适用于
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 -> decimal
Public Shared Function Parse (s As String, provider As IFormatProvider) As Decimal
参数
- s
- String
要转换的数字的字符串表示形式。
- provider
- IFormatProvider
一个 IFormatProvider,它提供有关 s
的区域性特定分析信息。
返回
Decimal 数,它与由 s
所指定的 provider
中包含的数字等效。
实现
例外
s
为 null
。
s
的格式不正确。
s
表示小于 Decimal.MinValue 或大于 Decimal.MaxValue 的数字。
示例
以下示例是 Web 窗体的按钮单击事件处理程序。 它使用 属性返回的 HttpRequest.UserLanguages 数组来确定用户的区域设置。 然后,它实例化与 CultureInfo 该区域设置对应的 对象。 NumberFormatInfo然后,将属于该CultureInfo对象的 对象传递给 方法,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]
方括号 ([ and ]) 中的元素是可选的。 下表对每个元素进行了描述。
元素 | 说明 |
---|---|
ws | 可选空格。 |
sign | 可选符号。 |
位数 | 从 0 到 9 的数字序列。 |
, | 区域性特定的千位分隔符。 |
. | 区域性特定的小数点符号。 |
fractional-digits | 从 0 到 9 的数字序列。 |
参数 s
是使用 样式解释的 NumberStyles.Number 。 这意味着允许使用空格和数千个分隔符,但不允许使用货币符号。 若要显式定义 (元素,如货币符号、千位分隔符和空白) 中可能存在 s
,请使用 Decimal.Parse(String, NumberStyles, IFormatProvider) 方法。
参数 provider
是实现 IFormatProvider ,如 NumberFormatInfo 或 CultureInfo 对象。 参数 provider
提供分析中使用的特定于区域性的信息。 如果 provider
为 null
,则使用当前区域性。
对象的 Decimal 精度为 29 位。 如果 s
表示的数字超过 29 位,但具有小数部分,并且位于 和 MinValue的范围内MaxValue,则使用舍入到最接近的数字将数字舍入而不是截断为 29 位数字。
如果在分析操作期间在 参数中 s
遇到分隔符,并且适用的货币或数字十进制分隔符和组分隔符相同,则分析操作假定该分隔符是小数分隔符而不是组分隔符。 有关分隔符的详细信息,请参阅 CurrencyDecimalSeparator、 NumberDecimalSeparator、 CurrencyGroupSeparator和 NumberGroupSeparator。
另请参阅
适用于
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 -> 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
参数
- 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 -> 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
参数
- s
- ReadOnlySpan<Char>
一个范围,包含表示要转换的数字的字符。
- style
- NumberStyles
NumberStyles 值的按位组合,指示可出现在 s
中的样式元素。 要指定的一个典型值为 Number。
- provider
- IFormatProvider
一个 IFormatProvider 对象,用于提供有关 s
格式的区域性特定信息。
返回
Decimal 数,它与 s
和 style
所指定的 provider
中包含的数字等效。
实现
适用于
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 -> decimal
Public 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
格式的区域性特定信息。
返回
Decimal 数,它与 s
和 style
所指定的 provider
中包含的数字等效。
实现
例外
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]
方括号 ([ and ]) 中的元素是可选的。 下表对每个元素进行了描述。
元素 | 说明 |
---|---|
$ | 区域性特定的货币符号。 它在字符串中的位置由 CurrencyNegativePattern 参数的 方法provider 返回GetFormat的 NumberFormatInfo 对象的 和 CurrencyPositivePattern 属性定义。 如果style 包含 标志,NumberStyles.AllowCurrencySymbol则可以显示s 货币符号。 |
ws | 可选空格。 如果 style 包含标志,则空格可以出现在 开头s ,如果style 包含NumberStyles.AllowTrailingWhite标志,则它可能显示在 s NumberStyles.AllowLeadingWhite 末尾。 |
sign | 可选符号。 如果style 包含 标志,则符号可以出现在 的s 开头,如果style 包含 NumberStyles.AllowTrailingSign 标志,则它可以显示在 s NumberStyles.AllowLeadingSign 末尾。 如果style 包含 NumberStyles.AllowParentheses 标志,则可以在 中使用s 括号来指示负值。 |
位数 | 数字序列,范围从 0 到 9。 |
, | 区域性特定的千位分隔符。 如果style 包含 标志,则 由 provider 定义的区域性的千位分隔符会显示在 NumberStyles.AllowThousands 中s 。 |
. | 区域性特定的小数点符号。 如果包含 标志,则 由 provider 定义的区域性的小数点符号会显示在 中s 。NumberStyles.AllowDecimalPointstyle |
fractional-digits | 数字序列,范围从 0 到 9。 仅当包含 NumberStyles.AllowDecimalPoint 标志时style ,小数位数才能显示在 中s 。 |
e | “e”或“E”字符,指示值以指数表示法表示。 如果style 包含 标志,参数s 可以表示指数表示法的数字NumberStyles.AllowExponent。 |
注意
分析操作将忽略中 s
任何 (U+0000) 字符的终止 NUL,而不考虑参数的值 style
。
仅包含数字的字符串 (对应于 None 样式) 在类型范围内 Decimal 时始终成功分析。 其余 NumberStyles 成员控制元素,这些元素可能位于输入字符串中,但不需要存在。 下表指示各个 NumberStyles 成员如何影响 中可能存在的 s
元素。
NumberStyles 值 | 除数字外允许的元素 |
---|---|
None | 仅限 digits 元素。 |
AllowDecimalPoint | 和小数位数元素。 |
AllowExponent | 参数 s 还可以使用指数表示法。 此标志支持数字 E位数形式的值;若要成功分析包含正数或负号和小数点符号等元素的字符串,需要其他标志。 |
AllowLeadingWhite | 开头的 s ws 元素。 |
AllowTrailingWhite | 末尾的 s ws 元素。 |
AllowLeadingSign | 开头的 s sign 元素。 |
AllowTrailingSign | 末尾的 s sign 元素。 |
AllowParentheses | 以括号形式将数值括起来的 符号 元素。 |
AllowThousands | 、 元素。 |
AllowCurrencySymbol | $ 元素。 |
Currency | 全部。 参数 s 不能表示十六进制数或指数表示法中的数字。 |
Float |
ws 元素位于 开头或末尾s ,符号位于 的s 开头,符号为 。 参数 s 还可以使用指数表示法。 |
Number |
ws 、sign 、、、和 。 |
Any | 除 之外 s 的所有样式都不能表示十六进制数。 |
参数 provider
是一个 IFormatProvider 实现,例如 NumberFormatInfo 或 CultureInfo 对象。 参数 provider
提供分析中使用的特定于区域性的信息。 如果 provider
为 null
,则使用当前区域性。
对象的 Decimal 精度为 29 位。 如果s
表示的数字超过 29 位,但具有小数部分,并且位于 和 MinValue的范围内MaxValue,则使用舍入到最接近的数字将数字舍入(而不是截断)为 29 位。
如果在分析操作期间参数中 s
遇到分隔符,并且适用的货币或数字小数分隔符和组分隔符相同,则分析操作假定分隔符是小数点分隔符,而不是组分隔符。 有关分隔符的详细信息,请参阅 CurrencyDecimalSeparator、 NumberDecimalSeparator、 CurrencyGroupSeparator和 NumberGroupSeparator。
另请参阅
适用于
反馈
https://aka.ms/ContentUserFeedback。
即将发布:在整个 2024 年,我们将逐步淘汰作为内容反馈机制的“GitHub 问题”,并将其取代为新的反馈系统。 有关详细信息,请参阅:提交和查看相关反馈