Int16.TryParse 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
将数字的字符串表示形式转换为它的等效 16 位有符号整数。 一个指示转换是否成功的返回值。
重载
TryParse(ReadOnlySpan<Byte>, IFormatProvider, Int16) |
尝试将 UTF-8 字符范围解析为值。 |
TryParse(ReadOnlySpan<Char>, Int16) |
将指定样式和区域性特定格式的数字的范围表示形式转换为它的等效 16 位带符号整数。 一个指示转换是否成功的返回值。 |
TryParse(String, Int16) |
将数字的字符串表示形式转换为它的等效 16 位有符号整数。 一个指示转换是否成功的返回值。 |
TryParse(ReadOnlySpan<Char>, IFormatProvider, Int16) |
尝试将字符范围解析为值。 |
TryParse(String, IFormatProvider, Int16) |
尝试将字符串分析为值。 |
TryParse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider, Int16) |
尝试将 UTF-8 字符范围解析为值。 |
TryParse(ReadOnlySpan<Byte>, Int16) |
尝试将包含数字字符串表示形式的 UTF-8 字符范围转换为其等效的 16 位带符号整数。 |
TryParse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider, Int16) |
将指定样式和区域性特定格式的数字的范围表示形式转换为它的等效 16 位带符号整数。 一个指示转换是否成功的返回值。 |
TryParse(String, NumberStyles, IFormatProvider, Int16) |
将指定样式和区域性特定格式的数字的字符串表示形式转换为它的等效 16 位有符号整数。 一个指示转换是否成功的返回值。 |
TryParse(ReadOnlySpan<Byte>, IFormatProvider, Int16)
- Source:
- Int16.cs
- Source:
- Int16.cs
尝试将 UTF-8 字符范围解析为值。
public:
static bool TryParse(ReadOnlySpan<System::Byte> utf8Text, IFormatProvider ^ provider, [Runtime::InteropServices::Out] short % result) = IUtf8SpanParsable<short>::TryParse;
public static bool TryParse (ReadOnlySpan<byte> utf8Text, IFormatProvider? provider, out short result);
static member TryParse : ReadOnlySpan<byte> * IFormatProvider * int16 -> bool
Public Shared Function TryParse (utf8Text As ReadOnlySpan(Of Byte), provider As IFormatProvider, ByRef result As Short) As Boolean
参数
- utf8Text
- ReadOnlySpan<Byte>
要分析的 UTF-8 字符范围。
- provider
- IFormatProvider
一个对象,提供有关 utf8Text
的区域性特定格式设置信息。
- result
- Int16
返回时,包含成功分析 utf8Text
的结果或失败时未定义的值。
返回
true
如果 utf8Text
已成功分析,则为 ;否则为 false
。
适用于
TryParse(ReadOnlySpan<Char>, Int16)
- Source:
- Int16.cs
- Source:
- Int16.cs
- Source:
- Int16.cs
将指定样式和区域性特定格式的数字的范围表示形式转换为它的等效 16 位带符号整数。 一个指示转换是否成功的返回值。
public:
static bool TryParse(ReadOnlySpan<char> s, [Runtime::InteropServices::Out] short % result);
public static bool TryParse (ReadOnlySpan<char> s, out short result);
static member TryParse : ReadOnlySpan<char> * int16 -> bool
Public Shared Function TryParse (s As ReadOnlySpan(Of Char), ByRef result As Short) As Boolean
参数
- s
- ReadOnlySpan<Char>
一个范围,包含表示要转换的数字的字符。
- result
- Int16
当此方法返回时,如果转换成功,则包含与 s
中所包含数字等效的 16 位有符号整数值;如果转换失败,则为零。 如果参数为 null
或 Empty、的格式不符合 style
,或者表示小于 Int16.MinValue 或大于 Int16.MaxValue 的数字,则转换失败。s
此参数未经初始化即进行传递;最初在 result
中提供的任何值都会被覆盖。
返回
如果 true
成功转换,则为 s
;否则为 false
。
适用于
TryParse(String, Int16)
- Source:
- Int16.cs
- Source:
- Int16.cs
- Source:
- Int16.cs
将数字的字符串表示形式转换为它的等效 16 位有符号整数。 一个指示转换是否成功的返回值。
public:
static bool TryParse(System::String ^ s, [Runtime::InteropServices::Out] short % result);
public static bool TryParse (string s, out short result);
public static bool TryParse (string? s, out short result);
static member TryParse : string * int16 -> bool
Public Shared Function TryParse (s As String, ByRef result As Short) As Boolean
参数
- s
- String
包含要转换的数字的字符串。
- result
- Int16
当此方法返回时,如果转换成功,则包含与 s
中所包含数字等效的 16 位有符号整数值;如果转换失败,则为零。 如果参数为 null
或 Empty,格式不正确,或者表示的数字小于 Int16.MinValue 或大于 Int16.MaxValue,则转换失败。s
此参数未经初始化即进行传递;最初在 result
中提供的任何值都会被覆盖。
返回
如果 true
成功转换,则为 s
;否则为 false
。
示例
以下示例使用许多不同的字符串值调用 Int16.TryParse(String, Int16) 方法。
using System;
public class StringParsing
{
public static void Main()
{
TryToParse(null);
TryToParse("16051");
TryToParse("9432.0");
TryToParse("16,667");
TryToParse(" -322 ");
TryToParse("+4302");
TryToParse("(100);");
TryToParse("01FA");
}
private static void TryToParse(string value)
{
short number;
bool result = Int16.TryParse(value, out number);
if (result)
{
Console.WriteLine("Converted '{0}' to {1}.", value, number);
}
else
{
if (value == null) value = "";
Console.WriteLine("Attempted conversion of '{0}' failed.", value);
}
}
}
// The example displays the following output to the console:
// Attempted conversion of '' failed.
// Converted '16051' to 16051.
// Attempted conversion of '9432.0' failed.
// Attempted conversion of '16,667' failed.
// Converted ' -322 ' to -322.
// Converted '+4302' to 4302.
// Attempted conversion of '(100)' failed.
// Attempted conversion of '01FA' failed.
open System
let tryToParse (value: string) =
match Int16.TryParse value with
| true, number -> printfn "Converted '{value}' to {number}."
| _ ->
let value = if isNull value then "" else value
printfn $"Attempted conversion of '{value}' failed."
tryToParse null
tryToParse "16051"
tryToParse "9432.0"
tryToParse "16,667"
tryToParse " -322 "
tryToParse "+4302"
tryToParse "(100);"
tryToParse "01FA"
// The example displays the following output to the console:
// Attempted conversion of '' failed.
// Converted '16051' to 16051.
// Attempted conversion of '9432.0' failed.
// Attempted conversion of '16,667' failed.
// Converted ' -322 ' to -322.
// Converted '+4302' to 4302.
// Attempted conversion of '(100)' failed.
// Attempted conversion of '01FA' failed.
Module StringParsing
Public Sub Main()
TryToParse(Nothing)
TryToParse("16051")
TryToParse("9432.0")
TryToParse("16,667")
TryToParse(" -322 ")
TryToParse("+4302")
TryToParse("(100)")
TryToParse("01FA")
End Sub
Private Sub TryToParse(value As String)
Dim number As Int16
Dim result As Boolean = Int16.TryParse(value, number)
If result Then
Console.WriteLine("Converted '{0}' to {1}.", value, number)
Else
If value Is Nothing Then value = ""
Console.WriteLine("Attempted conversion of '{0}' failed.", value)
End If
End Sub
End Module
' The example displays the following output to the console:
' Attempted conversion of '' failed.
' Converted '16051' to 16051.
' Attempted conversion of '9432.0' failed.
' Attempted conversion of '16,667' failed.
' Converted ' -322 ' to -322.
' Converted '+4302' to 4302.
' Attempted conversion of '(100)' failed.
' Attempted conversion of '01FA' failed.
在此示例中, TryParse(String, Int16) 方法无法转换的一些字符串包括:
"9432.0". 转换失败,因为字符串不能包含小数分隔符;它只能包含整型数字。
"16,667". 转换失败,因为字符串不能包含组分隔符;它只能包含整型数字。
"(100)". 转换失败,因为字符串不能包含除当前区域性 NumberFormatInfo.NegativeSign 的 和 NumberFormatInfo.NumberNegativePattern 属性定义的负号以外的负号。
“01FA”。 转换失败,因为字符串不能包含十六进制数字;它只能包含十进制数字。
注解
方法 Int16.TryParse(String, Int16) 与 方法的不同之处在于 Int16.Parse(String) ,返回一个布尔值,该值指示分析操作是否成功,而不是返回分析后 Int16 的值。 它无需使用异常处理来测试 FormatException 无效且无法成功分析的 事件 s
。
参数 s
应为数字的字符串表示形式:
[ws][sign]digits[ws]
方括号中的项 ([ 和 ]) 是可选的。 下表对每个元素进行了描述。
元素 | 说明 |
---|---|
ws | 可选空格。 |
sign | 可选符号。 |
位数 | 从 0 到 9 的数字序列。 |
参数 s
是使用 样式解释的 NumberStyles.Integer 。 除了十进制数字之外,只允许使用前导空格和尾随空格以及前导符号。 若要显式定义样式元素以及中可能存在 s
的区域性特定格式设置信息, Int16.TryParse(String, NumberStyles, IFormatProvider, Int16) 请使用 方法。
参数 s
是使用针对当前系统区域性初始化的 对象中的 NumberFormatInfo 格式设置信息进行分析的。 有关详细信息,请参阅 CurrentInfo。
方法的 TryParse 此重载将 参数中的所有 s
数字解释为十进制数字。 若要分析十六进制数的字符串表示形式,请 Int16.TryParse(String, NumberStyles, IFormatProvider, Int16) 调用 重载。
另请参阅
- Parse(String)
- ToString()
- 在 .NET 中分析数值字符串
- 示例:.NET Core WinForms 格式设置实用工具 (C#)
- 示例:.NET Core WinForms 格式设置实用工具 (Visual Basic)
适用于
TryParse(ReadOnlySpan<Char>, IFormatProvider, Int16)
- Source:
- Int16.cs
- Source:
- Int16.cs
- Source:
- Int16.cs
尝试将字符范围解析为值。
public:
static bool TryParse(ReadOnlySpan<char> s, IFormatProvider ^ provider, [Runtime::InteropServices::Out] short % result) = ISpanParsable<short>::TryParse;
public static bool TryParse (ReadOnlySpan<char> s, IFormatProvider? provider, out short result);
static member TryParse : ReadOnlySpan<char> * IFormatProvider * int16 -> bool
Public Shared Function TryParse (s As ReadOnlySpan(Of Char), provider As IFormatProvider, ByRef result As Short) As Boolean
参数
- s
- ReadOnlySpan<Char>
要分析的字符范围。
- provider
- IFormatProvider
一个对象,提供有关 s
的区域性特定格式设置信息。
- result
- Int16
此方法返回时,包含成功分析 s
的结果或失败时的未定义值。
返回
true
如果 s
已成功分析,则为 ;否则为 false
。
适用于
TryParse(String, IFormatProvider, Int16)
- Source:
- Int16.cs
- Source:
- Int16.cs
- Source:
- Int16.cs
尝试将字符串分析为值。
public:
static bool TryParse(System::String ^ s, IFormatProvider ^ provider, [Runtime::InteropServices::Out] short % result) = IParsable<short>::TryParse;
public static bool TryParse (string? s, IFormatProvider? provider, out short result);
static member TryParse : string * IFormatProvider * int16 -> bool
Public Shared Function TryParse (s As String, provider As IFormatProvider, ByRef result As Short) As Boolean
参数
- s
- String
要分析的字符串。
- provider
- IFormatProvider
一个对象,提供有关 s
的区域性特定格式设置信息。
- result
- Int16
此方法返回时,包含成功分析 s
的结果或失败时的未定义值。
返回
true
如果 s
已成功分析,则为 ;否则为 false
。
适用于
TryParse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider, Int16)
- Source:
- Int16.cs
- Source:
- Int16.cs
尝试将 UTF-8 字符范围解析为值。
public:
static bool TryParse(ReadOnlySpan<System::Byte> utf8Text, System::Globalization::NumberStyles style, IFormatProvider ^ provider, [Runtime::InteropServices::Out] short % result) = System::Numerics::INumberBase<short>::TryParse;
public static bool TryParse (ReadOnlySpan<byte> utf8Text, System.Globalization.NumberStyles style, IFormatProvider? provider, out short result);
static member TryParse : ReadOnlySpan<byte> * System.Globalization.NumberStyles * IFormatProvider * int16 -> bool
Public Shared Function TryParse (utf8Text As ReadOnlySpan(Of Byte), style As NumberStyles, provider As IFormatProvider, ByRef result As Short) As Boolean
参数
- utf8Text
- ReadOnlySpan<Byte>
要分析的 UTF-8 字符范围。
- style
- NumberStyles
可以存在于 中的 utf8Text
数字样式的按位组合。
- provider
- IFormatProvider
一个对象,提供有关 utf8Text
的区域性特定格式设置信息。
- result
- Int16
返回时,包含成功分析 utf8Text
的结果或失败时未定义的值。
返回
true
如果 utf8Text
已成功分析,则为 ;否则为 false
。
适用于
TryParse(ReadOnlySpan<Byte>, Int16)
- Source:
- Int16.cs
- Source:
- Int16.cs
尝试将包含数字字符串表示形式的 UTF-8 字符范围转换为其等效的 16 位带符号整数。
public:
static bool TryParse(ReadOnlySpan<System::Byte> utf8Text, [Runtime::InteropServices::Out] short % result);
public static bool TryParse (ReadOnlySpan<byte> utf8Text, out short result);
static member TryParse : ReadOnlySpan<byte> * int16 -> bool
Public Shared Function TryParse (utf8Text As ReadOnlySpan(Of Byte), ByRef result As Short) As Boolean
参数
- utf8Text
- ReadOnlySpan<Byte>
包含表示要转换的数字的 UTF-8 字符的跨度。
- result
- Int16
此方法返回时,如果转换成功,则包含与 中包含的 utf8Text
数字等效的 16 位带符号整数值;如果转换失败,则包含零。 此参数未经初始化即进行传递;最初在 result 中提供的任何值都会被覆盖。
返回
如果 true
成功转换,则为 utf8Text
;否则为 false
。
适用于
TryParse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider, Int16)
- Source:
- Int16.cs
- Source:
- Int16.cs
- Source:
- Int16.cs
将指定样式和区域性特定格式的数字的范围表示形式转换为它的等效 16 位带符号整数。 一个指示转换是否成功的返回值。
public:
static bool TryParse(ReadOnlySpan<char> s, System::Globalization::NumberStyles style, IFormatProvider ^ provider, [Runtime::InteropServices::Out] short % result);
public:
static bool TryParse(ReadOnlySpan<char> s, System::Globalization::NumberStyles style, IFormatProvider ^ provider, [Runtime::InteropServices::Out] short % result) = System::Numerics::INumberBase<short>::TryParse;
public static bool TryParse (ReadOnlySpan<char> s, System.Globalization.NumberStyles style, IFormatProvider? provider, out short result);
public static bool TryParse (ReadOnlySpan<char> s, System.Globalization.NumberStyles style, IFormatProvider provider, out short result);
static member TryParse : ReadOnlySpan<char> * System.Globalization.NumberStyles * IFormatProvider * int16 -> bool
Public Shared Function TryParse (s As ReadOnlySpan(Of Char), style As NumberStyles, provider As IFormatProvider, ByRef result As Short) As Boolean
参数
- s
- ReadOnlySpan<Char>
一个范围,包含表示要转换的数字的字符。 该范围使用由 style
指定的样式来进行解释。
- style
- NumberStyles
枚举值的按位组合,用于指示可出现在 s
中的样式元素。 要指定的一个典型值为 Integer。
- provider
- IFormatProvider
一个对象,提供有关 s
的区域性特定格式设置信息。
- result
- Int16
当此方法返回时,如果转换成功,则包含与 s
中所包含数字等效的 16 位有符号整数值;如果转换失败,则为零。 如果参数为 null
或 Empty、的格式不符合 style
,或者表示小于 Int16.MinValue 或大于 Int16.MaxValue 的数字,则转换失败。s
此参数未经初始化即进行传递;最初在 result
中提供的任何值都会被覆盖。
返回
如果 true
成功转换,则为 s
;否则为 false
。
适用于
TryParse(String, NumberStyles, IFormatProvider, Int16)
- Source:
- Int16.cs
- Source:
- Int16.cs
- Source:
- Int16.cs
将指定样式和区域性特定格式的数字的字符串表示形式转换为它的等效 16 位有符号整数。 一个指示转换是否成功的返回值。
public:
static bool TryParse(System::String ^ s, System::Globalization::NumberStyles style, IFormatProvider ^ provider, [Runtime::InteropServices::Out] short % result);
public:
static bool TryParse(System::String ^ s, System::Globalization::NumberStyles style, IFormatProvider ^ provider, [Runtime::InteropServices::Out] short % result) = System::Numerics::INumberBase<short>::TryParse;
public static bool TryParse (string s, System.Globalization.NumberStyles style, IFormatProvider provider, out short result);
public static bool TryParse (string? s, System.Globalization.NumberStyles style, IFormatProvider? provider, out short result);
static member TryParse : string * System.Globalization.NumberStyles * IFormatProvider * int16 -> bool
Public Shared Function TryParse (s As String, style As NumberStyles, provider As IFormatProvider, ByRef result As Short) As Boolean
参数
- s
- String
包含要转换的数字的字符串。 该字符串使用由 style
指定的样式来进行解释。
- style
- NumberStyles
枚举值的按位组合,用于指示可出现在 s
中的样式元素。 要指定的一个典型值为 Integer。
- provider
- IFormatProvider
一个对象,提供有关 s
的区域性特定格式设置信息。
- result
- Int16
当此方法返回时,如果转换成功,则包含与 s
中所包含数字等效的 16 位有符号整数值;如果转换失败,则为零。 如果参数为 null
或 Empty、的格式不符合 style
,或者表示小于 Int16.MinValue 或大于 Int16.MaxValue 的数字,则转换失败。s
此参数未经初始化即进行传递;最初在 result
中提供的任何值都会被覆盖。
返回
如果 true
成功转换,则为 s
;否则为 false
。
例外
示例
以下示例使用许多不同的字符串值调用 Int16.TryParse(String, NumberStyles, IFormatProvider, Int16) 方法。
using System;
using System.Globalization;
public class StringParsing
{
public static void Main()
{
string numericString;
NumberStyles styles;
numericString = "10677";
styles = NumberStyles.Integer;
CallTryParse(numericString, styles);
numericString = "-30677";
styles = NumberStyles.None;
CallTryParse(numericString, styles);
numericString = "10345.00";
styles = NumberStyles.Integer | NumberStyles.AllowDecimalPoint;
CallTryParse(numericString, styles);
numericString = "10345.72";
styles = NumberStyles.Integer | NumberStyles.AllowDecimalPoint;
CallTryParse(numericString, styles);
numericString = "22,593";
styles = NumberStyles.Integer | NumberStyles.AllowThousands;
CallTryParse(numericString, styles);
numericString = "12E-01";
styles = NumberStyles.Integer | NumberStyles.AllowExponent;
CallTryParse(numericString, styles);
numericString = "12E03";
CallTryParse(numericString, styles);
numericString = "80c1";
CallTryParse(numericString, NumberStyles.HexNumber);
numericString = "0x80C1";
CallTryParse(numericString, NumberStyles.HexNumber);
}
private static void CallTryParse(string stringToConvert, NumberStyles styles)
{
short number;
bool result = Int16.TryParse(stringToConvert, styles,
CultureInfo.InvariantCulture, out number);
if (result)
Console.WriteLine($"Converted '{stringToConvert}' to {number}.");
else
Console.WriteLine($"Attempted conversion of '{stringToConvert}' failed.");
}
}
// The example displays the following output to the console:
// Converted '10677' to 10677.
// Attempted conversion of '-30677' failed.
// Converted '10345.00' to 10345.
// Attempted conversion of '10345.72' failed.
// Converted '22,593' to 22593.
// Attempted conversion of '12E-01' failed.
// Converted '12E03' to 12000.
// Converted '80c1' to -32575.
// Attempted conversion of '0x80C1' failed.
open System
open System.Globalization
let callTryParse (stringToConvert: string) (styles: NumberStyles) =
match Int16.TryParse(stringToConvert, styles, CultureInfo.InvariantCulture) with
| true, number ->
printfn $"Converted '{stringToConvert}' to {number}."
| _ ->
printfn $"Attempted conversion of '{stringToConvert}' failed."
[<EntryPoint>]
let main _ =
let numericString = "10677"
let styles = NumberStyles.Integer
callTryParse numericString styles
let numericString = "-30677"
let styles = NumberStyles.None
callTryParse numericString styles
let numericString = "10345.00"
let styles = NumberStyles.Integer ||| NumberStyles.AllowDecimalPoint
callTryParse numericString styles
let numericString = "10345.72";
let styles = NumberStyles.Integer ||| NumberStyles.AllowDecimalPoint
callTryParse numericString styles
let numericString = "22,593"
let styles = NumberStyles.Integer ||| NumberStyles.AllowThousands
callTryParse numericString styles
let numericString = "12E-01"
let styles = NumberStyles.Integer ||| NumberStyles.AllowExponent
callTryParse numericString styles
let numericString = "12E03"
callTryParse numericString styles
let numericString = "80c1"
callTryParse numericString NumberStyles.HexNumber
let numericString = "0x80C1"
callTryParse numericString NumberStyles.HexNumber
0
// The example displays the following output to the console:
// Converted '10677' to 10677.
// Attempted conversion of '-30677' failed.
// Converted '10345.00' to 10345.
// Attempted conversion of '10345.72' failed.
// Converted '22,593' to 22593.
// Attempted conversion of '12E-01' failed.
// Converted '12E03' to 12000.
// Converted '80c1' to -32575.
// Attempted conversion of '0x80C1' failed.
Imports System.Globalization
Module StringParsing
Public Sub Main()
Dim numericString As String
Dim styles As NumberStyles
numericString = "10677"
styles = NumberStyles.Integer
CallTryParse(numericString, styles)
numericString = "-30677"
styles = NumberStyles.None
CallTryParse(numericString, styles)
numericString = "10345.00"
styles = NumberStyles.Integer Or NumberStyles.AllowDecimalPoint
CallTryParse(numericString, styles)
numericString = "10345.72"
styles = NumberStyles.Integer Or NumberStyles.AllowDecimalPoint
CallTryParse(numericString, styles)
numericString = "22,593"
styles = NumberStyles.Integer Or NumberStyles.AllowThousands
CallTryParse(numericString, styles)
numericString = "12E-01"
styles = NumberStyles.Integer Or NumberStyles.AllowExponent
CallTryParse(numericString, styles)
numericString = "12E03"
CallTryParse(numericString, styles)
numericString = "80c1"
CallTryParse(numericString, NumberStyles.HexNumber)
numericString = "0x80C1"
CallTryParse(numericString, NumberStyles.HexNumber)
End Sub
Private Sub CallTryParse(stringToConvert As String, styles AS NumberStyles)
Dim number As Short
Dim result As Boolean = Int16.TryParse(stringToConvert, styles, _
CultureInfo.InvariantCulture, number)
If result Then
Console.WriteLine("Converted '{0}' to {1}.", stringToConvert, number)
Else
Console.WriteLine("Attempted conversion of '{0}' failed.", _
Convert.ToString(stringToConvert))
End If
End Sub
End Module
' The example displays the following output to the console:
' Converted '10677' to 10677.
' Attempted conversion of '-30677' failed.
' Converted '10345.00' to 10345.
' Attempted conversion of '10345.72' failed.
' Converted '22,593' to 22593.
' Attempted conversion of '12E-01' failed.
' Converted '12E03' to 12000.
' Converted '80c1' to -32575.
' Attempted conversion of '0x80C1' failed.
注解
方法 Int16.TryParse(String, NumberStyles, IFormatProvider, Int16) 与 方法的不同之处在于 Int16.Parse(String, NumberStyles, IFormatProvider) ,返回一个布尔值,该值指示分析操作是否成功,而不是返回分析后 Int16 的值。 它无需使用异常处理来测试 FormatException 无效且无法成功分析的 事件 s
。
参数 style
定义样式元素 (,如空格或正或负号) ,这些元素在 参数中 s
允许分析操作成功。 它必须是 枚举中的位标志 NumberStyles 的组合。 根据 style 的值, s
参数可能包括以下元素:
[ws][$][sign][digits,]digits[.fractional_digits][e[sign]digits][ws]
或者,如果 style
参数包含 NumberStyles.AllowHexSpecifier:
[ws]hexdigits[ws]
方括号中的项 ([ 和 ]) 是可选的。 下表对每个元素进行了描述。
元素 | 说明 |
---|---|
ws | 可选空格。 如果包含 标志,则的s 开头会出现空格;如果style style 包含 NumberStyles.AllowLeadingWhiteNumberStyles.AllowTrailingWhite 标志,则可以在 的s 末尾显示空格。 |
$ | 区域性特定的货币符号。 它在字符串中的位置由 CurrencyPositivePattern 参数的 方法provider 返回GetFormat的 NumberFormatInfo 对象的 属性定义。 如果style 包含 标志,NumberStyles.AllowCurrencySymbol则可以在 中s 显示货币符号。 |
sign | 可选符号。 |
位数 | 从 0 到 9 的数字序列。 |
, | 区域性特定的千位分隔符。 如果包含 标志,则 指定的provider 区域性的千位分隔符可以出现在 中s 。NumberStyles.AllowThousandsstyle |
. | 区域性特定的小数点符号。 如果包含 标志,则 指定的provider 区域性的小数点符号可以出现在 中s 。NumberStyles.AllowDecimalPointstyle |
fractional_digits | 出现一个或多个数字 0。 仅当包含 标志时style ,NumberStyles.AllowDecimalPoint小数位数才能显示在 中s 。 |
e | “e”或“E”字符,指示该值以指数表示法表示。 如果style 包含 标志,则 s 参数可以表示指数表示法中的NumberStyles.AllowExponent数字。 |
hexdigits | 从 0 到 f 或 0 到 F 的十六进制数字序列。 |
注意
分析操作将忽略中 s
任何 (U+0000) 字符的终止 NUL,而不考虑 参数的值 style
。
仅具有十进制数字的字符串 (对应于 NumberStyles.None 标志) 始终成功分析。 大多数剩余 NumberStyles 成员控制可能在此输入字符串中存在但不需要存在的元素。 下表指示各个 NumberStyles 成员如何影响 中可能存在 s
的元素。
非复合 NumberStyles 值 | 除了数字外,还允许 在 中 |
---|---|
NumberStyles.None | 仅十进制数字。 |
NumberStyles.AllowDecimalPoint | 和fractional_digits 元素。 但是, fractional_digits 只能包含一个或多个 0 位数字,否则方法将 false 返回 。 |
NumberStyles.AllowExponent | 参数 s 还可以使用指数表示法。 如果 s 以指数表示法表示数字,则它必须表示数据类型范围内 Int16 没有非零小数部分的整数。 |
NumberStyles.AllowLeadingWhite | 开头的ws 元素。 |
NumberStyles.AllowTrailingWhite | 位于 末尾的ws 元素。 |
NumberStyles.AllowLeadingSign | 符号可以显示在 数字之前。 |
NumberStyles.AllowTrailingSign | 数字 后面会显示一个符号。 |
NumberStyles.AllowParentheses | 用括号括住数值的 符号 元素。 |
NumberStyles.AllowThousands | 、 元素。 |
NumberStyles.AllowCurrencySymbol | $ 元素。 |
NumberStyles.Currency | 所有元素。 s 参数不能表示十六进制数或指数表示法中的数字。 |
NumberStyles.Float | 位于 开头或末尾的 s ws元素,符号位于 的s 开头,符号为 。 参数 s 还可以使用指数表示法。 |
NumberStyles.Number | ws、符号、千位分隔符 (、) 和小数点 (.) 元素。 |
NumberStyles.Any | 所有样式(除外 s )都不能表示十六进制数。 |
如果使用标志 NumberStyles.AllowHexSpecifier , s
必须是不带前缀的十六进制值。 例如,“9AF3”分析成功,但“0x9AF3”未成功。 中唯一可以存在的 style
其他标志是 NumberStyles.AllowLeadingWhite 和 NumberStyles.AllowTrailingWhite。 (NumberStyles 枚举具有复合样式 NumberStyles.HexNumber,其中包括空格标志 )
参数 provider
是实现 IFormatProvider ,如 CultureInfo 对象或 NumberFormatInfo 对象,其 GetFormat 方法返回 对象 NumberFormatInfo 。 对象 NumberFormatInfo 提供有关 格式的 s
区域性特定信息。 如果 provider
为 null
,则 NumberFormatInfo 使用当前区域性的 对象。
另请参阅
适用于
反馈
https://aka.ms/ContentUserFeedback。
即将发布:在整个 2024 年,我们将逐步淘汰作为内容反馈机制的“GitHub 问题”,并将其取代为新的反馈系统。 有关详细信息,请参阅:提交和查看相关反馈