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<byte> utf8Text, out decimal result);

参数

utf8Text
ReadOnlySpan<Byte>

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

result
Decimal

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

返回

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

适用于

.NET 10 和其他版本
产品 版本
.NET 8, 9, 10

TryParse(ReadOnlySpan<Char>, Decimal)

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

使用区域性特定的格式将数字的跨度表示形式转换为其等效 Decimal。 返回值指示转换是成功还是失败。

public static bool TryParse(ReadOnlySpan<char> s, out decimal result);

参数

s
ReadOnlySpan<Char>

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

result
Decimal

此方法返回时,如果转换成功,则包含与 s中包含的数值等效的 Decimal 数字;如果转换失败,则包含零。 如果 s 参数 nullEmpty 或表示小于 Decimal.MinValue 或大于 Decimal.MaxValue的数字,则转换失败。 此参数传递为 uininitialized;最初在 result 中提供的任何值将被覆盖。

返回

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

适用于

.NET 10 和其他版本
产品 版本
.NET Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Standard 2.1

TryParse(String, Decimal)

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

将数字的字符串表示形式转换为其等效 Decimal。 返回值指示转换是成功还是失败。

public static bool TryParse(string s, out decimal result);
public static bool TryParse(string? s, out decimal result);

参数

s
String

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

result
Decimal

此方法返回时,如果转换成功,则包含与 s中包含的数值等效的 Decimal 数字;如果转换失败,则包含零。 如果 s 参数 nullEmpty、不是有效格式的数字,或者表示小于 Decimal.MinValue 或大于 Decimal.MaxValue的数字,则转换失败。 此参数传递为 uininitialized;最初在 result 中提供的任何值将被覆盖。

返回

如果已成功转换 s,则 true;否则,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

注解

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

参数 s 包含多种形式:

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

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

元素 描述
ws 可选空格。
签名 可选符号。
一系列数字,范围从 0 到 9。
区域性特定的千位分隔符符号。
区域性特定的小数点符号。
小数位数 一系列数字,范围从 0 到 9。

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

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

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

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

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

另请参阅

适用于

.NET 10 和其他版本
产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

TryParse(ReadOnlySpan<Byte>, IFormatProvider, Decimal)

Source:
Decimal.cs
Source:
Decimal.cs

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

public static bool TryParse(ReadOnlySpan<byte> utf8Text, IFormatProvider? provider, out decimal result);

参数

utf8Text
ReadOnlySpan<Byte>

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

provider
IFormatProvider

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

result
Decimal

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

返回

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

适用于

.NET 10 和其他版本
产品 版本
.NET 8, 9, 10

TryParse(ReadOnlySpan<Char>, IFormatProvider, Decimal)

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

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

public static bool TryParse(ReadOnlySpan<char> s, IFormatProvider? provider, out decimal result);

参数

s
ReadOnlySpan<Char>

要分析的字符范围。

provider
IFormatProvider

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

result
Decimal

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

返回

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

适用于

.NET 10 和其他版本
产品 版本
.NET 7, 8, 9, 10

TryParse(String, IFormatProvider, Decimal)

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

尝试将字符串分析为值。

public static bool TryParse(string? s, IFormatProvider? provider, out decimal result);

参数

s
String

要分析的字符串。

provider
IFormatProvider

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

result
Decimal

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

返回

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

适用于

.NET 10 和其他版本
产品 版本
.NET 7, 8, 9, 10

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

Source:
Decimal.cs
Source:
Decimal.cs

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

public static bool TryParse(ReadOnlySpan<byte> utf8Text, System.Globalization.NumberStyles style, IFormatProvider? provider, out decimal result);

参数

utf8Text
ReadOnlySpan<Byte>

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

style
NumberStyles

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

provider
IFormatProvider

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

result
Decimal

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

返回

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

适用于

.NET 10 和其他版本
产品 版本
.NET 8, 9, 10

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, out decimal result);
public static bool TryParse(ReadOnlySpan<char> s, System.Globalization.NumberStyles style, IFormatProvider provider, out decimal result);

参数

s
ReadOnlySpan<Char>

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

style
NumberStyles

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

provider
IFormatProvider

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

result
Decimal

此方法返回时,如果转换成功,则包含与 s中包含的数值等效的 Decimal 数字;如果转换失败,则包含零。 如果 s 参数是 nullEmpty,则转换失败,不是符合 style格式的数字,或者表示小于 Decimal.MinValue 或大于 Decimal.MaxValue的数字。 此参数传递为 uininitialized;最初在 result 中提供的任何值将被覆盖。

返回

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

适用于

.NET 10 和其他版本
产品 版本
.NET Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Standard 2.1

TryParse(String, NumberStyles, IFormatProvider, Decimal)

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

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

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);

参数

s
String

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

style
NumberStyles

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

provider
IFormatProvider

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

result
Decimal

此方法返回时,如果转换成功,则包含与 s中包含的数值等效的 Decimal 数字;如果转换失败,则包含零。 如果 s 参数是 nullEmpty,则转换失败,不是符合 style格式的数字,或者表示小于 Decimal.MinValue 或大于 Decimal.MaxValue的数字。 此参数传递为 uininitialized;最初在 result 中提供的任何值将被覆盖。

返回

如果已成功转换 s,则 true;否则,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'.

注解

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

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

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

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

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

元素 描述
ws 可选空格。 如果 style 包含 NumberStyles.AllowLeadingWhite 标志,则空格可能会显示在 s 的开头。 如果 style 包含 NumberStyles.AllowTrailingWhite 标志,则它可以显示在 s 末尾。
$ 区域性特定的货币符号。 字符串中的位置由 provider 参数 IFormatProvider.GetFormat 方法返回的 NumberFormatInfo 对象的 NumberFormatInfo.CurrencyNegativePatternNumberFormatInfo.CurrencyPositivePattern 属性定义。 如果 style 包含 NumberStyles.AllowCurrencySymbol 标志,货币符号可以出现在 s 中。
签名 可选符号。
一系列数字,范围从 0 到 9。
区域性特定的小数点符号。
小数位数 一系列数字,范围从 0 到 9。

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

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

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

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

另请参阅

适用于

.NET 10 和其他版本
产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0