Decimal.TryParse 方法

定义

将数字的字符串表示形式转换为它的等效 Decimal 表示形式。 一个指示转换是否成功的返回值。

重载

TryParse(ReadOnlySpan<Byte>, Decimal)

尝试将包含数字字符串表示形式的 UTF-8 字符范围转换为其等效的带符号十进制。

TryParse(ReadOnlySpan<Char>, Decimal)

使用指定样式和区域性特定格式将数字的范围表示形式转换为其 Decimal 等效项。 一个指示转换是否成功的返回值。

TryParse(String, Decimal)

将数字的字符串表示形式转换为它的等效 Decimal 表示形式。 一个指示转换是否成功的返回值。

TryParse(ReadOnlySpan<Byte>, IFormatProvider, Decimal)

尝试将 UTF-8 字符范围解析为值。

TryParse(ReadOnlySpan<Char>, IFormatProvider, Decimal)

尝试将字符范围解析为值。

TryParse(String, IFormatProvider, Decimal)

尝试将字符串分析为值。

TryParse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider, Decimal)

尝试将 UTF-8 字符范围解析为值。

TryParse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider, Decimal)

使用指定样式和区域性特定格式将数字的范围表示形式转换为其 Decimal 等效项。 一个指示转换是否成功的返回值。

TryParse(String, NumberStyles, IFormatProvider, Decimal)

使用指定样式和区域性特定格式将数字的字符串表示形式转换为其 Decimal 等效项。 一个指示转换是否成功的返回值。

TryParse(ReadOnlySpan<Byte>, Decimal)

Source:
Decimal.cs
Source:
Decimal.cs

尝试将包含数字字符串表示形式的 UTF-8 字符范围转换为其等效的带符号十进制。

public:
 static bool TryParse(ReadOnlySpan<System::Byte> utf8Text, [Runtime::InteropServices::Out] System::Decimal % result);
public static bool TryParse (ReadOnlySpan<byte> utf8Text, out decimal result);
static member TryParse : ReadOnlySpan<byte> * decimal -> bool
Public Shared Function TryParse (utf8Text As ReadOnlySpan(Of Byte), ByRef result As Decimal) As Boolean

参数

utf8Text
ReadOnlySpan<Byte>

包含表示要转换的数字的 UTF-8 字符的跨度。

result
Decimal

此方法返回时,如果转换成功,则包含与 中包含的 utf8Text 数字等效的带符号十进制值;如果转换失败,则包含零。 此参数未经初始化即进行传递;最初在 result 中提供的任何值都会被覆盖。

返回

如果 true 成功转换,则为 utf8Text;否则为 false

适用于

TryParse(ReadOnlySpan<Char>, Decimal)

Source:
Decimal.cs
Source:
Decimal.cs
Source:
Decimal.cs

使用指定样式和区域性特定格式将数字的范围表示形式转换为其 Decimal 等效项。 一个指示转换是否成功的返回值。

public:
 static bool TryParse(ReadOnlySpan<char> s, [Runtime::InteropServices::Out] System::Decimal % result);
public static bool TryParse (ReadOnlySpan<char> s, out decimal result);
static member TryParse : ReadOnlySpan<char> * decimal -> bool
Public Shared Function TryParse (s As ReadOnlySpan(Of Char), ByRef result As Decimal) As Boolean

参数

s
ReadOnlySpan<Char>

一个范围,包含表示要转换的数字的字符。

result
Decimal

如果转换成功,此方法返回与 s 中所含数值相当的 Decimal 数;如果转换失败,此方法返回零。 如果 s 参数为 nullEmpty,不是格式符合 style的数字,或者表示小于 Decimal.MinValue 或大于 Decimal.MaxValue 的数字,则转换失败。 此参数未经初始化即进行传递;result 中最初提供的任何值都会遭覆盖。

返回

如果 true 成功转换,则为 s;否则为 false

适用于

TryParse(String, Decimal)

Source:
Decimal.cs
Source:
Decimal.cs
Source:
Decimal.cs

将数字的字符串表示形式转换为它的等效 Decimal 表示形式。 一个指示转换是否成功的返回值。

public:
 static bool TryParse(System::String ^ s, [Runtime::InteropServices::Out] System::Decimal % result);
public static bool TryParse (string s, out decimal result);
public static bool TryParse (string? s, out decimal result);
static member TryParse : string * decimal -> bool
Public Shared Function TryParse (s As String, ByRef result As Decimal) As Boolean

参数

s
String

要转换的数字的字符串表示形式。

result
Decimal

如果转换成功,此方法返回与 s 中所含数值相当的 Decimal 数;如果转换失败,此方法返回零。 如果 s 参数为 nullEmpty,不是有效格式的数字,或者表示小于 Decimal.MinValue 或大于 Decimal.MaxValue 的数字,则转换失败。 此参数未经初始化即进行传递;result 中最初提供的任何值都会遭覆盖。

返回

如果 true 成功转换,则为 s;否则为 false

示例

以下示例使用 Decimal.TryParse(String, Decimal) 方法将数值的字符串表示形式转换为 Decimal 值。 它假定 en-US 是当前区域性。

string value;
decimal number;

// Parse a floating-point value with a thousands separator.
value = "1,643.57";
if (Decimal.TryParse(value, out number))
   Console.WriteLine(number);
else
   Console.WriteLine("Unable to parse '{0}'.", value);

// Parse a floating-point value with a currency symbol and a
// thousands separator.
value = "$1,643.57";
if (Decimal.TryParse(value, out number))
   Console.WriteLine(number);
else
   Console.WriteLine("Unable to parse '{0}'.", value);

// Parse value in exponential notation.
value = "-1.643e6";
if (Decimal.TryParse(value, out number))
   Console.WriteLine(number);
else
   Console.WriteLine("Unable to parse '{0}'.", value);

// Parse a negative integer value.
value = "-1689346178821";
if (Decimal.TryParse(value, out number))
   Console.WriteLine(number);
else
   Console.WriteLine("Unable to parse '{0}'.", value);
// The example displays the following output to the console:
//       1643.57
//       Unable to parse '$1,643.57'.
//       Unable to parse '-1.643e6'.
//       -1689346178821
// Parse a floating-point value with a thousands separator.
let value = "1,643.57"
match Decimal.TryParse value with
| true, number ->
    printfn $"{number}"
| _ ->
    printfn $"Unable to parse '{value}'."

// Parse a floating-point value with a currency symbol and a
// thousands separator.
let value = "$1,643.57"
match Decimal.TryParse value with
| true, number ->
    printfn $"{number}"
| _ -> 
    printfn $"Unable to parse '{value}'."

// Parse value in exponential notation.
let value = "-1.643e6"
match Decimal.TryParse value with
| true, number ->
    printfn $"{number}"
| _ -> 
    printfn $"Unable to parse '{value}'."

// Parse a negative integer value.
let value = "-1689346178821"
match Decimal.TryParse value with
| true, number ->
    printfn $"{number}"
| _ -> 
    printfn $"Unable to parse '{value}'."
// The example displays the following output to the console:
//       1643.57
//       Unable to parse '$1,643.57'.
//       Unable to parse '-1.643e6'.
//       -1689346178821
Dim value As String
Dim number As Decimal

' Parse a floating-point value with a thousands separator.
value = "1,643.57"
If Decimal.TryParse(value, number) Then
   Console.WriteLine(number)
Else
   Console.WriteLine("Unable to parse '{0}'.", value)      
End If   

' Parse a floating-point value with a currency symbol and a 
' thousands separator.
value = "$1,643.57"
If Decimal.TryParse(value, number) Then
   Console.WriteLine(number)  
Else
   Console.WriteLine("Unable to parse '{0}'.", value)   
End If

' Parse value in exponential notation.
value = "-1.643e6"
If Decimal.TryParse(value, number)
   Console.WriteLine(number)
Else
   Console.WriteLine("Unable to parse '{0}'.", value)   
End If

' Parse a negative integer value.
value = "-1689346178821"
If Decimal.TryParse(value, number)
   Console.WriteLine(number)
Else
   Console.WriteLine("Unable to parse '{0}'.", value)   
End If
' The example displays the following output to the console:
'       1643.57
'       Unable to parse '$1,643.57'.
'       Unable to parse '-1.643e6'.
'       -1689346178821

注解

此重载不同于 Decimal.Parse(String) 方法,返回一个布尔值,该值指示分析操作是否成功,而不是返回已分析的数值。 它无需使用异常处理来测试 FormatException 无效且无法成功分析的 事件 s

参数 s 包含以下形式的数字:

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

方括号 ([ and ]) 中的元素是可选的。 下表对每个元素进行了描述。

元素 说明
ws 可选空格。
sign 可选符号。
位数 从 0 到 9 的数字序列。
, 区域性特定的千位分隔符。
. 区域性特定的小数点符号。
fractional-digits 从 0 到 9 的数字序列。

参数 s 是使用 样式解释的 NumberStyles.Number 。 这意味着允许使用空格和数千个分隔符,但不允许使用货币符号。 若要显式定义 (元素,如货币符号、千位分隔符和空白) 中可能存在 s,请使用 Decimal.TryParse(String, NumberStyles, IFormatProvider, Decimal) 方法重载。

参数 s 是使用为当前系统区域性初始化的 对象中的 NumberFormatInfo 格式设置信息进行分析的。 有关详细信息,请参阅 CurrentInfo。 若要使用其他指定区域性的格式设置信息分析字符串,请使用 Decimal.TryParse(String, NumberStyles, IFormatProvider, Decimal) 方法重载。

如有必要,使用舍入到最接近值对 的值 s 进行舍入。

对象的 Decimal 精度为 29 位。 如果 s 表示的数字超过 29 位,但具有小数部分,并且位于 和 MinValue的范围内MaxValue,则使用舍入到最接近的数字将数字舍入而不是截断为 29 位数字。

如果在分析操作期间在 参数中 s 遇到分隔符,并且适用的货币或数字十进制分隔符和组分隔符相同,则分析操作假定该分隔符是小数分隔符而不是组分隔符。 有关分隔符的详细信息,请参阅 CurrencyDecimalSeparatorNumberDecimalSeparatorCurrencyGroupSeparatorNumberGroupSeparator

另请参阅

适用于

TryParse(ReadOnlySpan<Byte>, IFormatProvider, Decimal)

Source:
Decimal.cs
Source:
Decimal.cs

尝试将 UTF-8 字符范围解析为值。

public:
 static bool TryParse(ReadOnlySpan<System::Byte> utf8Text, IFormatProvider ^ provider, [Runtime::InteropServices::Out] System::Decimal % result) = IUtf8SpanParsable<System::Decimal>::TryParse;
public static bool TryParse (ReadOnlySpan<byte> utf8Text, IFormatProvider? provider, out decimal result);
static member TryParse : ReadOnlySpan<byte> * IFormatProvider * decimal -> bool
Public Shared Function TryParse (utf8Text As ReadOnlySpan(Of Byte), provider As IFormatProvider, ByRef result As Decimal) As Boolean

参数

utf8Text
ReadOnlySpan<Byte>

要分析的 UTF-8 字符范围。

provider
IFormatProvider

一个对象,提供有关 utf8Text 的区域性特定格式设置信息。

result
Decimal

返回时,包含成功分析 utf8Text 的结果或失败时未定义的值。

返回

true 如果 utf8Text 已成功分析,则为 ;否则为 false

适用于

TryParse(ReadOnlySpan<Char>, IFormatProvider, Decimal)

Source:
Decimal.cs
Source:
Decimal.cs
Source:
Decimal.cs

尝试将字符范围解析为值。

public:
 static bool TryParse(ReadOnlySpan<char> s, IFormatProvider ^ provider, [Runtime::InteropServices::Out] System::Decimal % result) = ISpanParsable<System::Decimal>::TryParse;
public static bool TryParse (ReadOnlySpan<char> s, IFormatProvider? provider, out decimal result);
static member TryParse : ReadOnlySpan<char> * IFormatProvider * decimal -> bool
Public Shared Function TryParse (s As ReadOnlySpan(Of Char), provider As IFormatProvider, ByRef result As Decimal) As Boolean

参数

s
ReadOnlySpan<Char>

要分析的字符范围。

provider
IFormatProvider

一个对象,提供有关 s 的区域性特定格式设置信息。

result
Decimal

此方法返回时,包含成功分析 s的结果或失败时的未定义值。

返回

true 如果 s 已成功分析,则为 ;否则为 false

适用于

TryParse(String, IFormatProvider, Decimal)

Source:
Decimal.cs
Source:
Decimal.cs
Source:
Decimal.cs

尝试将字符串分析为值。

public:
 static bool TryParse(System::String ^ s, IFormatProvider ^ provider, [Runtime::InteropServices::Out] System::Decimal % result) = IParsable<System::Decimal>::TryParse;
public static bool TryParse (string? s, IFormatProvider? provider, out decimal result);
static member TryParse : string * IFormatProvider * decimal -> bool
Public Shared Function TryParse (s As String, provider As IFormatProvider, ByRef result As Decimal) As Boolean

参数

s
String

要分析的字符串。

provider
IFormatProvider

一个对象,提供有关 s 的区域性特定格式设置信息。

result
Decimal

此方法返回时,包含成功分析 s 的结果或失败时的未定义值。

返回

true 如果 s 已成功分析,则为 ;否则为 false

适用于

TryParse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider, Decimal)

Source:
Decimal.cs
Source:
Decimal.cs

尝试将 UTF-8 字符范围解析为值。

public:
 static bool TryParse(ReadOnlySpan<System::Byte> utf8Text, System::Globalization::NumberStyles style, IFormatProvider ^ provider, [Runtime::InteropServices::Out] System::Decimal % result) = System::Numerics::INumberBase<System::Decimal>::TryParse;
public static bool TryParse (ReadOnlySpan<byte> utf8Text, System.Globalization.NumberStyles style, IFormatProvider? provider, out decimal result);
static member TryParse : ReadOnlySpan<byte> * System.Globalization.NumberStyles * IFormatProvider * decimal -> bool
Public Shared Function TryParse (utf8Text As ReadOnlySpan(Of Byte), style As NumberStyles, provider As IFormatProvider, ByRef result As Decimal) As Boolean

参数

utf8Text
ReadOnlySpan<Byte>

要分析的 UTF-8 字符范围。

style
NumberStyles

可以存在于 中的 utf8Text数字样式的按位组合。

provider
IFormatProvider

一个对象,提供有关 utf8Text 的区域性特定格式设置信息。

result
Decimal

返回时,包含成功分析 utf8Text 的结果或失败时未定义的值。

返回

true 如果 utf8Text 已成功分析,则为 ;否则为 false

适用于

TryParse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider, Decimal)

Source:
Decimal.cs
Source:
Decimal.cs
Source:
Decimal.cs

使用指定样式和区域性特定格式将数字的范围表示形式转换为其 Decimal 等效项。 一个指示转换是否成功的返回值。

public:
 static bool TryParse(ReadOnlySpan<char> s, System::Globalization::NumberStyles style, IFormatProvider ^ provider, [Runtime::InteropServices::Out] System::Decimal % result);
public:
 static bool TryParse(ReadOnlySpan<char> s, System::Globalization::NumberStyles style, IFormatProvider ^ provider, [Runtime::InteropServices::Out] System::Decimal % result) = System::Numerics::INumberBase<System::Decimal>::TryParse;
public static bool TryParse (ReadOnlySpan<char> s, System.Globalization.NumberStyles style, IFormatProvider? provider, out decimal result);
public static bool TryParse (ReadOnlySpan<char> s, System.Globalization.NumberStyles style, IFormatProvider provider, out decimal result);
static member TryParse : ReadOnlySpan<char> * System.Globalization.NumberStyles * IFormatProvider * decimal -> bool
Public Shared Function TryParse (s As ReadOnlySpan(Of Char), style As NumberStyles, provider As IFormatProvider, ByRef result As Decimal) As Boolean

参数

s
ReadOnlySpan<Char>

一个范围,包含表示要转换的数字的字符。

style
NumberStyles

枚举值的一个按位组合,指示 s 所允许的格式。 要指定的一个典型值为 Number

provider
IFormatProvider

一个对象,它提供有关 s 的区域性特定分析信息。

result
Decimal

如果转换成功,此方法返回与 s 中所含数值相当的 Decimal 数;如果转换失败,此方法返回零。 如果 s 参数为 nullEmpty,不是格式符合 style的数字,或者表示小于 Decimal.MinValue 或大于 Decimal.MaxValue 的数字,则转换失败。 此参数未经初始化即进行传递;result 中最初提供的任何值都会遭覆盖。

返回

如果 true 成功转换,则为 s;否则为 false

适用于

TryParse(String, NumberStyles, IFormatProvider, Decimal)

Source:
Decimal.cs
Source:
Decimal.cs
Source:
Decimal.cs

使用指定样式和区域性特定格式将数字的字符串表示形式转换为其 Decimal 等效项。 一个指示转换是否成功的返回值。

public:
 static bool TryParse(System::String ^ s, System::Globalization::NumberStyles style, IFormatProvider ^ provider, [Runtime::InteropServices::Out] System::Decimal % result);
public:
 static bool TryParse(System::String ^ s, System::Globalization::NumberStyles style, IFormatProvider ^ provider, [Runtime::InteropServices::Out] System::Decimal % result) = System::Numerics::INumberBase<System::Decimal>::TryParse;
public static bool TryParse (string s, System.Globalization.NumberStyles style, IFormatProvider provider, out decimal result);
public static bool TryParse (string? s, System.Globalization.NumberStyles style, IFormatProvider? provider, out decimal result);
static member TryParse : string * System.Globalization.NumberStyles * IFormatProvider * decimal -> bool
Public Shared Function TryParse (s As String, style As NumberStyles, provider As IFormatProvider, ByRef result As Decimal) As Boolean

参数

s
String

要转换的数字的字符串表示形式。

style
NumberStyles

枚举值的一个按位组合,指示 s 所允许的格式。 要指定的一个典型值为 Number

provider
IFormatProvider

一个对象,它提供有关 s 的区域性特定分析信息。

result
Decimal

如果转换成功,此方法返回与 s 中所含数值相当的 Decimal 数;如果转换失败,此方法返回零。 如果 s 参数为 nullEmpty,不是格式符合 style的数字,或者表示小于 Decimal.MinValue 或大于 Decimal.MaxValue 的数字,则转换失败。 此参数未经初始化即进行传递;result 中最初提供的任何值都会遭覆盖。

返回

如果 true 成功转换,则为 s;否则为 false

例外

style 不是 NumberStyles 值。

styleAllowHexSpecifier 值。

示例

下面的示例演示如何使用 TryParse(String, NumberStyles, IFormatProvider, Decimal) 方法分析具有特定样式并使用特定区域性约定设置格式的数字的字符串表示形式。

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

// Parse currency value using en-GB culture.
value = "£1,097.63";
style = NumberStyles.Number | NumberStyles.AllowCurrencySymbol;
culture = CultureInfo.CreateSpecificCulture("en-GB");
if (Decimal.TryParse(value, style, culture, out number))
   Console.WriteLine("Converted '{0}' to {1}.", value, number);
else
   Console.WriteLine("Unable to convert '{0}'.", value);
// Displays:
//       Converted '£1,097.63' to 1097.63.

value = "1345,978";
style = NumberStyles.AllowDecimalPoint;
culture = CultureInfo.CreateSpecificCulture("fr-FR");
if (Decimal.TryParse(value, style, culture, out number))
   Console.WriteLine("Converted '{0}' to {1}.", value, number);
else
   Console.WriteLine("Unable to convert '{0}'.", value);
// Displays:
//       Converted '1345,978' to 1345.978.

value = "1.345,978";
style = NumberStyles.AllowDecimalPoint | NumberStyles.AllowThousands;
culture = CultureInfo.CreateSpecificCulture("es-ES");
if (Decimal.TryParse(value, style, culture, out number))
   Console.WriteLine("Converted '{0}' to {1}.", value, number);
else
   Console.WriteLine("Unable to convert '{0}'.", value);
// Displays:
//       Converted '1.345,978' to 1345.978.

value = "1 345,978";
if (Decimal.TryParse(value, style, culture, out number))
   Console.WriteLine("Converted '{0}' to {1}.", value, number);
else
   Console.WriteLine("Unable to convert '{0}'.", value);
// Displays:
//       Unable to convert '1 345,978'.
// Parse currency value using en-GB culture.
let value = "£1,097.63"
let style = NumberStyles.Number ||| NumberStyles.AllowCurrencySymbol
let culture = CultureInfo.CreateSpecificCulture "en-GB"
match Decimal.TryParse(value, style, culture) with
| true, number ->
    printfn $"Converted '{value}' to {number}."
| _ -> 
    printfn $"Unable to convert '{value}'."
// Displays:
//       Converted '£1,097.63' to 1097.63.

let value = "1345,978"
let style = NumberStyles.AllowDecimalPoint
let culture = CultureInfo.CreateSpecificCulture "fr-FR"
match Decimal.TryParse(value, style, culture) with
| true, number ->
    printfn $"Converted '{value}' to {number}."
| _ -> 
    printfn $"Unable to convert '{value}'."
// Displays:
//       Converted '1345,978' to 1345.978.

let value = "1.345,978"
let style = NumberStyles.AllowDecimalPoint ||| NumberStyles.AllowThousands
let culture = CultureInfo.CreateSpecificCulture "es-ES"
match Decimal.TryParse(value, style, culture) with
| true, number ->
    printfn $"Converted '{value}' to {number}."
| _ -> 
    printfn $"Unable to convert '{value}'."
// Displays:
//       Converted '1.345,978' to 1345.978.

let value = "1 345,978"
match Decimal.TryParse(value, style, culture) with
| true, number ->
    printfn $"Converted '{value}' to {number}."
| _ -> 
    printfn $"Unable to convert '{value}'."
// Displays:
//       Unable to convert '1 345,978'.
Dim value As String
Dim style As NumberStyles
Dim culture As CultureInfo
Dim number As Decimal

' Parse currency value using en-GB culture.
value = "£1,097.63"
style = NumberStyles.Number Or NumberStyles.AllowCurrencySymbol
culture = CultureInfo.CreateSpecificCulture("en-GB")
If Decimal.TryParse(value, style, culture, number) Then
   Console.WriteLine("Converted '{0}' to {1}.", value, number)
Else
   Console.WriteLine("Unable to convert '{0}'.", value)
End If    
' Displays: 
'       Converted '£1,097.63' to 1097.63.

value = "1345,978"
style = NumberStyles.AllowDecimalPoint
culture = CultureInfo.CreateSpecificCulture("fr-FR")
If Decimal.TryParse(value, style, culture, number) Then
   Console.WriteLine("Converted '{0}' to {1}.", value, number)
Else
   Console.WriteLine("Unable to convert '{0}'.", value)
End If    
' Displays:
'       Converted '1345,978' to 1345.978.

value = "1.345,978"
style = NumberStyles.AllowDecimalPoint Or NumberStyles.AllowThousands
culture = CultureInfo.CreateSpecificCulture("es-ES")
If Decimal.TryParse(value, style, culture, number) Then
   Console.WriteLine("Converted '{0}' to {1}.", value, number)
Else
   Console.WriteLine("Unable to convert '{0}'.", value)
End If    
' Displays: 
'       Converted '1.345,978' to 1345.978.

value = "1 345,978"
If Decimal.TryParse(value, style, culture, number) Then
   Console.WriteLine("Converted '{0}' to {1}.", value, number)
Else
   Console.WriteLine("Unable to convert '{0}'.", value)
End If    
' Displays:
'       Unable to convert '1 345,978'.

注解

此重载不同于 Decimal.Parse(String, NumberStyles, IFormatProvider) 方法,返回一个布尔值,该值指示分析操作是否成功,而不是返回已分析的数值。 它无需使用异常处理来测试 FormatException 无效且无法成功分析的 事件 s

参数 style 定义允许的参数格式, s 以便分析操作成功。 它必须是 枚举中的位标志 NumberStyles 的组合。 不支持以下 NumberStyles 成员:

根据 style 的值, s 参数可能包括以下元素:

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

方括号 ([ and ]) 中的元素是可选的。 下表对每个元素进行了描述。

元素 说明
ws 可选空格。 如果style包含 NumberStyles.AllowLeadingWhite 标志,s则 开头会出现空格。 如果style包含 标志,NumberStyles.AllowTrailingWhite则它可以显示在 的末尾s
$ 区域性特定的货币符号。 它在字符串中的位置由 NumberFormatInfo.CurrencyNegativePattern 参数的 方法provider返回IFormatProvider.GetFormatNumberFormatInfo 对象的 或 NumberFormatInfo.CurrencyPositivePattern 属性定义。 如果style包含 标志,NumberStyles.AllowCurrencySymbol则可以在 中s显示货币符号。
sign 可选符号。
位数 从 0 到 9 的数字序列。
. 区域性特定的小数点符号。
fractional-digits 从 0 到 9 的数字序列。

参数 style 指定参数的允许格式 s ,可以是使用按位 OR 运算组合的一个或多个 NumberStyles 枚举常量。 如果 style 为 null, s 则使用 NumberStyles.Number 样式进行解释。

参数 provider 是一个 IFormatProvider 实现,例如 NumberFormatInfoCultureInfo 对象。 参数 provider 提供分析中使用的特定于区域性的信息。 如果 providernull,则使用当前区域性。

对象的 Decimal 精度为 29 位。 如果 s 表示的数字超过 29 位,但具有小数部分,并且位于 和 MinValue的范围内MaxValue,则使用舍入到最接近的数字将数字舍入而不是截断为 29 位数字。

如果在分析操作期间在 参数中 s 遇到分隔符,并且适用的货币或数字十进制分隔符和组分隔符相同,则分析操作假定该分隔符是小数分隔符而不是组分隔符。 有关分隔符的详细信息,请参阅 CurrencyDecimalSeparatorNumberDecimalSeparatorCurrencyGroupSeparatorNumberGroupSeparator

另请参阅

适用于