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
- 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]
方括号 ([ 和 ]) 中的元素是可选的。 下表描述了每个元素。
| 元素 | 描述 |
|---|---|
| ws | 可选空格。 |
| 签名 | 可选符号。 |
| 数字 | 一系列数字,范围从 0 到 9。 |
| 、 | 区域性特定的千位分隔符符号。 |
| . | 区域性特定的小数点符号。 |
| fractional-digits | 一系列数字,范围从 0 到 9。 |
参数 s 使用 NumberStyles.Number 样式进行解释。 这意味着允许空格和数千个分隔符,但货币符号不是。 若要显式定义可以存在的 s元素(如货币符号、千位分隔符和空格),请使用 Decimal.Parse(String, NumberStyles) 或 Decimal.Parse(String, NumberStyles, IFormatProvider) 方法。
使用初始化的当前系统区域性中的NumberFormatInfo格式设置信息分析参数s。 有关详细信息,请参阅 CurrentInfo。 若要使用其他区域性的格式设置信息分析字符串,请使用 Decimal.Parse(String, IFormatProvider) 或 Decimal.Parse(String, NumberStyles, IFormatProvider) 方法。
如有必要, s 使用舍入到最接近的值进行舍入。
A Decimal 的精度为 29 位。 如果s表示数字的位数超过 29 位,但具有小数部分,并且位于该数字范围内MaxValueMinValue,并且该数字是舍入的,而不是截断的,则使用舍入到最接近的 29 位数字。
如果在分析操作期间,在参数中 s 遇到分隔符,并且适用的货币或数字小数和组分隔符相同,则分析操作假定分隔符是小数分隔符,而不是组分隔符。 有关分隔符的详细信息,请参阅CurrencyDecimalSeparator、NumberDecimalSeparator和CurrencyGroupSeparatorNumberGroupSeparator。
另请参阅
适用于
Parse(ReadOnlySpan<Byte>, IFormatProvider)
- Source:
- Decimal.cs
- Source:
- Decimal.cs
- 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
- 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
- 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所指定的style数字等效的数字s。
例外
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包含标志,则空白可以出现在标志的s开头,如果包含NumberStyles.AllowTrailingWhite标志,则它将显示在该标志的stylesNumberStyles.AllowLeadingWhite末尾。 |
| $ | 区域性特定的货币符号。 字符串中的位置由NumberFormatInfo.CurrencyNegativePatternNumberFormatInfo.CurrencyPositivePattern当前区域性的属性定义。 如果s包含标志,则当前区域性的货币符号可以出现在style其中NumberStyles.AllowCurrencySymbol。 |
| 签名 | 可选符号。 如果style包含标志,则符号可以出现在标志的s开头,如果包含NumberStyles.AllowTrailingSign标志,则它将显示在标志的sstyleNumberStyles.AllowLeadingSign末尾。 括号可用于 s 指示负值(如果 style 包含 NumberStyles.AllowParentheses 标志)。 |
| 数字 | 一系列数字,范围从 0 到 9。 |
| 、 | 区域性特定的千位分隔符符号。 如果style包含标志,则当前区域性的千位分隔符可以出现在s其中NumberStyles.AllowThousands。 |
| . | 区域性特定的小数点符号。 如果包含标志,则当前区域性的小数点符号可以出现在sstyle其中NumberStyles.AllowDecimalPoint。 |
| fractional-digits | 一系列数字,范围从 0 到 9。 仅当包含s标志时style,小数位数才会显示NumberStyles.AllowDecimalPoint。 |
| e | “e”或“E”字符,指示该值以指数表示法表示。 如果s包含标志,则参数style可以表示指数表示法中的NumberStyles.AllowExponent数字。 |
注意
无论参数的值s如何,分析作都会忽略任何终止 NUL (U+0000) 字符style。
仅包含数字(对应于样式)的 None 字符串在类型范围内 Decimal 时始终成功分析。 其余 NumberStyles 成员控制可能但不需要在输入字符串中存在的元素。 下表指示各个 NumberStyles 成员如何影响可能存在于 s的元素。
| NumberStyles 值 | 除数字外允许的元素 |
|---|---|
| None | 仅 数字 元素。 |
| AllowDecimalPoint | 和小数位数元素。 |
| AllowExponent | 该 s 参数还可以使用指数表示法。 此标志支持采用格式 数字E数字的值;需要其他标志才能成功分析包含正号或负号和小数点符号等元素的字符串。 |
| AllowLeadingWhite | 开头的 sws 元素。 |
| AllowTrailingWhite | 末尾的 s 元素 |
| AllowLeadingSign | 开头的 ssign 元素。 |
| AllowTrailingSign | 结尾处的 s 元素。 |
| AllowParentheses | 括住数值的括号形式的 符号 元素。 |
| AllowThousands | 元素。 |
| AllowCurrencySymbol | 元素 $ 。 |
| Currency | 都。 该 s 参数不能表示十六进制数或指数表示法中的数字。 |
| Float | 开头或末尾s的 ws元素,符号位于开头s,符号.。 该 s 参数还可以使用指数表示法。 |
| Number |
.
sign
,和ws元素。 |
| Any | 所有样式,但 s 不能表示十六进制数。 |
使用 s 为当前系统区域性初始化的对象中的 NumberFormatInfo 格式设置信息分析参数。 有关详细信息,请参阅 CurrentInfo。
A Decimal 的精度为 29 位。 如果s表示数字的位数超过 29 位,但具有小数部分,并且位于该数字范围内MaxValueMinValue,并且该数字是舍入的,而不是截断的,则使用舍入到最接近的 29 位数字。
如果在分析操作期间在参数中 s 遇到分隔符, styles 包括 NumberStyles.AllowThousands 和 NumberStyles.AllowDecimalPoint 值,并且适用的货币或数字小数和组分隔符是相同的,则分析操作假定分隔符是小数分隔符,而不是组分隔符。 有关分隔符的详细信息,请参阅CurrencyDecimalSeparator、NumberDecimalSeparator和CurrencyGroupSeparatorNumberGroupSeparator。
另请参阅
适用于
Parse(String, IFormatProvider)
- Source:
- Decimal.cs
- Source:
- Decimal.cs
- 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所指定的provider数字等效的数字s。
实现
例外
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]
方括号 ([ 和 ]) 中的元素是可选的。 下表描述了每个元素。
| 元素 | 描述 |
|---|---|
| ws | 可选空格。 |
| 签名 | 可选符号。 |
| 数字 | 一系列数字,范围从 0 到 9。 |
| 、 | 区域性特定的千位分隔符符号。 |
| . | 区域性特定的小数点符号。 |
| fractional-digits | 一系列数字,范围从 0 到 9。 |
参数 s 使用 NumberStyles.Number 样式进行解释。 这意味着允许空格和数千个分隔符,但货币符号不是。 若要显式定义可以存在的 s元素(如货币符号、千位分隔符和空格),请使用该方法 Decimal.Parse(String, NumberStyles, IFormatProvider) 。
参数 provider 是实现 IFormatProvider ,如 NumberFormatInfo 或 CultureInfo 对象。 该 provider 参数提供分析中使用的区域性特定信息。
null如果是provider,则使用线程当前区域性。
对象 Decimal 具有 29 位精度。 如果s表示数字的位数超过 29 位,但具有小数部分,并且位于该数字范围内MaxValueMinValue,并且该数字是舍入的,而不是截断的,则使用舍入到最接近的 29 位数字。
如果在分析操作期间在参数中 s 遇到分隔符,并且适用的货币或数字小数和组分隔符相同,则分析操作假定分隔符是小数分隔符,而不是组分隔符。 有关分隔符的详细信息,请参阅CurrencyDecimalSeparator、NumberDecimalSeparator和CurrencyGroupSeparatorNumberGroupSeparator。
另请参阅
适用于
Parse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider)
- Source:
- Decimal.cs
- Source:
- Decimal.cs
- 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
- 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由和provider指定的style数字中s所包含的数字等效的数字。
实现
适用于
Parse(String, NumberStyles, IFormatProvider)
- Source:
- Decimal.cs
- Source:
- Decimal.cs
- 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由和provider指定的style数字中s所包含的数字等效的数字。
实现
例外
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]
方括号 ([ 和 ]) 中的元素是可选的。 下表描述了每个元素。
| 元素 | 描述 |
|---|---|
| $ | 区域性特定的货币符号。 字符串中的位置由CurrencyNegativePatternCurrencyPositivePattern参数方法provider返回GetFormat的对象的属性NumberFormatInfo定义。 如果s包含标志,则货币符号可以出现在style其中NumberStyles.AllowCurrencySymbol。 |
| ws | 可选空格。 如果style包含标志,则空白可以出现在标志的s开头,如果包含NumberStyles.AllowTrailingWhite标志,则它将显示在该标志的stylesNumberStyles.AllowLeadingWhite末尾。 |
| 签名 | 可选符号。 如果style包含标志,则符号可以出现在标志的s开头,如果包含NumberStyles.AllowTrailingSign标志,则它将显示在标志的sstyleNumberStyles.AllowLeadingSign末尾。 括号可用于 s 指示负值(如果 style 包含 NumberStyles.AllowParentheses 标志)。 |
| 数字 | 一系列数字,范围从 0 到 9。 |
| 、 | 区域性特定的千位分隔符符号。 如果style包含NumberStyles.AllowThousands标志,则可以显示s所provider定义的区域性的千位分隔符。 |
| . | 区域性特定的小数点符号。 如果style包含NumberStyles.AllowDecimalPoint标志,则可以显示s由定义的provider区域性的小数点符号。 |
| fractional-digits | 一系列数字,范围从 0 到 9。 仅当包含s标志时style,小数位数才会显示NumberStyles.AllowDecimalPoint。 |
| e | “e”或“E”字符,指示该值以指数表示法表示。 如果s包含标志,则参数style可以表示指数表示法中的NumberStyles.AllowExponent数字。 |
注意
无论参数的值s如何,分析作都会忽略任何终止 NUL (U+0000) 字符style。
仅包含数字(对应于样式)的 None 字符串在类型范围内 Decimal 时始终成功分析。 其余 NumberStyles 成员控制可能但不需要在输入字符串中存在的元素。 下表指示各个 NumberStyles 成员如何影响可能存在于 s的元素。
| NumberStyles 值 | 除数字外允许的元素 |
|---|---|
| None | 仅 数字 元素。 |
| AllowDecimalPoint | 和小数位数元素。 |
| AllowExponent | 该 s 参数还可以使用指数表示法。 此标志支持采用格式 数字E数字的值;需要其他标志才能成功分析包含正号或负号和小数点符号等元素的字符串。 |
| AllowLeadingWhite | 开头的 sws 元素。 |
| AllowTrailingWhite | 末尾的 s 元素 |
| AllowLeadingSign | 开头的 ssign 元素。 |
| AllowTrailingSign | 结尾处的 s 元素。 |
| AllowParentheses | 括住数值的括号形式的 符号 元素。 |
| AllowThousands | 元素。 |
| AllowCurrencySymbol | 元素 $ 。 |
| Currency | 都。 该 s 参数不能表示十六进制数或指数表示法中的数字。 |
| Float | 开头或结尾s处的 ws 元素,符号位于开头s和 . 符号。 该 s 参数还可以使用指数表示法。 |
| Number | 、wssign、和 . 元素。 |
| Any | 所有样式,但 s 不能表示十六进制数。 |
参数 provider 是实现 IFormatProvider ,如 NumberFormatInfo 或 CultureInfo 对象。 该 provider 参数提供分析中使用的区域性特定信息。
null如果是provider,则使用线程当前区域性。
对象 Decimal 具有 29 位精度。 如果s表示数字的位数超过 29 位,但具有小数部分,并且位于该数字范围内MaxValueMinValue,并且该数字是舍入的,而不是截断的,则使用舍入到最接近的 29 位数字。
如果在分析操作期间在参数中 s 遇到分隔符,并且适用的货币或数字小数和组分隔符相同,则分析操作假定分隔符是小数分隔符,而不是组分隔符。 有关分隔符的详细信息,请参阅CurrencyDecimalSeparator、NumberDecimalSeparator和CurrencyGroupSeparatorNumberGroupSeparator。