Int64.Parse 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
将数字的字符串表示形式转换为它的等效 64 位有符号整数。
重载
Parse(String, NumberStyles, IFormatProvider) |
将指定样式和区域性特定格式的数字的字符串表示形式转换为它的等效 64 位有符号整数。 |
Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider) |
将指定样式和区域性特定格式的数字的范围表示形式转换为其等效的 64 位带符号整数。 |
Parse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider) |
将 UTF-8 字符范围分析为值。 |
Parse(String, IFormatProvider) |
将指定的区域性特定格式的数字的字符串表示形式转换为它的等效 64 位有符号整数。 |
Parse(String) |
将数字的字符串表示形式转换为它的等效 64 位有符号整数。 |
Parse(ReadOnlySpan<Char>, IFormatProvider) |
将字符范围解析为值。 |
Parse(ReadOnlySpan<Byte>, IFormatProvider) |
将 UTF-8 字符范围分析为值。 |
Parse(String, NumberStyles) |
将指定样式的数字的字符串表示形式转换为它的等效 64 位有符号整数。 |
Parse(String, NumberStyles, IFormatProvider)
- Source:
- Int64.cs
- Source:
- Int64.cs
- Source:
- Int64.cs
将指定样式和区域性特定格式的数字的字符串表示形式转换为它的等效 64 位有符号整数。
public:
static long Parse(System::String ^ s, System::Globalization::NumberStyles style, IFormatProvider ^ provider);
public:
static long Parse(System::String ^ s, System::Globalization::NumberStyles style, IFormatProvider ^ provider) = System::Numerics::INumberBase<long>::Parse;
public static long Parse (string s, System.Globalization.NumberStyles style, IFormatProvider provider);
public static long Parse (string s, System.Globalization.NumberStyles style, IFormatProvider? provider);
static member Parse : string * System.Globalization.NumberStyles * IFormatProvider -> int64
Public Shared Function Parse (s As String, style As NumberStyles, provider As IFormatProvider) As Long
参数
- s
- String
包含要转换的数字的字符串。
- style
- NumberStyles
枚举值的按位组合,用于指示可出现在 s
中的样式元素。 要指定的一个典型值为 Integer。
- provider
- IFormatProvider
一个 IFormatProvider,它提供有关 s
的区域性特定格式设置信息。
返回
与 s
中指定的数字等效的 64 位带符号整数。
实现
例外
s
为 null
。
s
的格式不符合 style
。
示例
以下示例使用各种 style
和 provider
参数来分析值的字符串表示形式 Int64 。 它还演示了一些不同的方法,根据其格式信息用于分析操作的区域性,可以解释同一字符串。
using System;
using System.Globalization;
public class ParseInt64
{
public static void Main()
{
Convert("12,000", NumberStyles.Float | NumberStyles.AllowThousands,
new CultureInfo("en-GB"));
Convert("12,000", NumberStyles.Float | NumberStyles.AllowThousands,
new CultureInfo("fr-FR"));
Convert("12,000", NumberStyles.Float, new CultureInfo("en-US"));
Convert("12 425,00", NumberStyles.Float | NumberStyles.AllowThousands,
new CultureInfo("sv-SE"));
Convert("12,425.00", NumberStyles.Float | NumberStyles.AllowThousands,
NumberFormatInfo.InvariantInfo);
Convert("631,900", NumberStyles.Integer | NumberStyles.AllowDecimalPoint,
new CultureInfo("fr-FR"));
Convert("631,900", NumberStyles.Integer | NumberStyles.AllowDecimalPoint,
new CultureInfo("en-US"));
Convert("631,900", NumberStyles.Integer | NumberStyles.AllowThousands,
new CultureInfo("en-US"));
}
private static void Convert(string value, NumberStyles style,
IFormatProvider provider)
{
try
{
long number = Int64.Parse(value, style, provider);
Console.WriteLine("Converted '{0}' to {1}.", value, number);
}
catch (FormatException)
{
Console.WriteLine("Unable to convert '{0}'.", value);
}
catch (OverflowException)
{
Console.WriteLine("'{0}' is out of range of the Int64 type.", value);
}
}
}
// This example displays the following output to the console:
// Converted '12,000' to 12000.
// Converted '12,000' to 12.
// Unable to convert '12,000'.
// Converted '12 425,00' to 12425.
// Converted '12,425.00' to 12425.
// '631,900' is out of range of the Int64 type.
// Unable to convert '631,900'.
// Converted '631,900' to 631900.
open System
open System.Globalization
let convert (value: string) style provider =
try
let number = Int64.Parse(value, style, provider)
printfn $"Converted '{value}' to {number}."
with
| :? FormatException ->
printfn $"Unable to convert '{value}'."
| :? OverflowException ->
printfn $"'{value}' is out of range of the Int64 type."
convert "12,000" (NumberStyles.Float ||| NumberStyles.AllowThousands) (CultureInfo "en-GB")
convert "12,000" (NumberStyles.Float ||| NumberStyles.AllowThousands) (CultureInfo "fr-FR")
convert "12,000" NumberStyles.Float (CultureInfo "en-US")
convert "12 425,00" (NumberStyles.Float ||| NumberStyles.AllowThousands) (CultureInfo "sv-SE")
convert "12,425.00" (NumberStyles.Float ||| NumberStyles.AllowThousands) NumberFormatInfo.InvariantInfo
convert "631,900" (NumberStyles.Integer ||| NumberStyles.AllowDecimalPoint) (CultureInfo "fr-FR")
convert "631,900" (NumberStyles.Integer ||| NumberStyles.AllowDecimalPoint) (CultureInfo "en-US")
convert "631,900" (NumberStyles.Integer ||| NumberStyles.AllowThousands) (CultureInfo "en-US")
// This example displays the following output to the console:
// Converted '12,000' to 12000.
// Converted '12,000' to 12.
// Unable to convert '12,000'.
// Converted '12 425,00' to 12425.
// Converted '12,425.00' to 12425.
// '631,900' is out of range of the Int64 type.
// Unable to convert '631,900'.
// Converted '631,900' to 631900.
Imports System.Globalization
Module ParseInt64
Public Sub Main()
Convert("12,000", NumberStyles.Float Or NumberStyles.AllowThousands, _
New CultureInfo("en-GB"))
Convert("12,000", NumberStyles.Float Or NumberStyles.AllowThousands, _
New CultureInfo("fr-FR"))
Convert("12,000", NumberStyles.Float, New CultureInfo("en-US"))
Convert("12 425,00", NumberStyles.Float Or NumberStyles.AllowThousands, _
New CultureInfo("sv-SE"))
Convert("12,425.00", NumberStyles.Float Or NumberStyles.AllowThousands, _
NumberFormatInfo.InvariantInfo)
Convert("631,900", NumberStyles.Integer Or NumberStyles.AllowDecimalPoint, _
New CultureInfo("fr-FR"))
Convert("631,900", NumberStyles.Integer Or NumberStyles.AllowDecimalPoint, _
New CultureInfo("en-US"))
Convert("631,900", NumberStyles.Integer Or NumberStyles.AllowThousands, _
New CultureInfo("en-US"))
End Sub
Private Sub Convert(value As String, style As NumberStyles, _
provider As IFormatProvider)
Try
Dim number As Long = Int64.Parse(value, style, provider)
Console.WriteLine("Converted '{0}' to {1}.", value, number)
Catch e As FormatException
Console.WriteLine("Unable to convert '{0}'.", value)
Catch e As OverflowException
Console.WriteLine("'{0}' is out of range of the Int64 type.", value)
End Try
End Sub
End Module
' This example displays the following output to the console:
' Converted '12,000' to 12000.
' Converted '12,000' to 12.
' Unable to convert '12,000'.
' Converted '12 425,00' to 12425.
' Converted '12,425.00' to 12425.
' '631,900' is out of range of the Int64 type.
' Unable to convert '631,900'.
' Converted '631,900' to 631900.
注解
参数 style
定义参数中 s
允许的样式元素 (,例如空格或正号) ,以便分析操作成功。 它必须是枚举中的位标志 NumberStyles 的组合。 根据 的值 style
, s
参数可能包含以下元素:
[ws][$][sign][digits,]digits[.fractional_digits][e[sign]exponential_digits][ws]
或者,如果 style
包括 AllowHexSpecifier:
[ws]hexdigits[ws]
方括号 ([ and ]) 中的元素是可选的。 下表对每个元素进行了描述。
元素 | 说明 |
---|---|
ws | 可选空格。 如果 style 包含标志,则空格可以出现在 开头s ,如果style 包含NumberStyles.AllowTrailingWhite标志,则它可能显示在 s NumberStyles.AllowLeadingWhite 末尾。 |
$ | 区域性特定的货币符号。 它在字符串中的位置由 NumberFormatInfo.CurrencyPositivePattern 参数的 方法provider 返回GetFormat的 NumberFormatInfo 对象的 属性定义。 如果style 包含 标志,NumberStyles.AllowCurrencySymbol则可以显示s 货币符号。 |
sign | 可选符号。 如果包含 NumberStyles.AllowLeadingSign 标志,则符号可以出现在 的s 开头,如果style style 包含NumberStyles.AllowTrailingSign标志,则显示在 的s 末尾。 如果style 包含 NumberStyles.AllowParentheses 标志,则可以在 中使用s 括号来指示负值。 |
位数 fractional_digits exponential_digits |
从 0 到 9 的数字序列。 |
, | 区域性特定的千位分隔符。 如果style 包含 标志,则 指定的provider 区域性的千位分隔符会显示在 NumberStyles.AllowThousands 中s 。 |
. | 区域性特定的小数点符号。 如果包含 标志,则 指定的provider 区域性的小数点符号可能会出现在 中s 。NumberStyles.AllowDecimalPointstyle 只有数字 0 可以显示为小数位数,以便分析操作成功;如果 fractional_digits 包含任何其他数字, OverflowException 则会引发 。 |
e | “e”或“E”字符,指示值以指数表示法表示。 如果style 包含 标志,参数s 可以表示指数表示法的数字NumberStyles.AllowExponent。 |
hexdigits | 从 0 到 f 或 0 到 F 的十六进制数字序列。 |
注意
分析操作将忽略中 s
任何 (U+0000) 字符的终止 NUL,而不考虑参数的值 style
。
具有十进制数字的字符串仅 (对应于 NumberStyles.None 样式) 在类型范围内 Int64 时始终成功分析。 大多数剩余 NumberStyles 成员控制元素,这些元素可能位于此输入字符串中,但不需要存在。 下表指示各个 NumberStyles 成员如何影响 中可能存在的 s
元素。
非复合 NumberStyles 值 | 除数字外允许的元素 |
---|---|
NumberStyles.None | 仅十进制数字。 |
NumberStyles.AllowDecimalPoint | 小数点 ( . ) 和 小数位数 元素。 但是, 小数位数 必须仅包含一个或多个 0 位,否则 OverflowException 会引发 。 |
NumberStyles.AllowExponent | 参数 s 还可以使用指数表示法。 |
NumberStyles.AllowLeadingWhite | 开头的 s ws 元素。 |
NumberStyles.AllowTrailingWhite | 末尾的 s ws 元素。 |
NumberStyles.AllowLeadingSign | 符号可以显示在 数字之前。 |
NumberStyles.AllowTrailingSign | 数字 后面可以显示一个符号。 |
NumberStyles.AllowParentheses | 以括号形式将数值括起来的 符号 元素。 |
NumberStyles.AllowThousands | 千位分隔符 ( , ) 元素。 |
NumberStyles.AllowCurrencySymbol | $ 元素。 |
如果使用标志 NumberStyles.AllowHexSpecifier , s
必须是不带前缀的十六进制值。 例如,“C9AF3”成功分析,但“0xC9AF3”不成功。 中唯一可以存在的 style
其他标志是 NumberStyles.AllowLeadingWhite 和 NumberStyles.AllowTrailingWhite。 (枚举 NumberStyles 具有复合数字样式, NumberStyles.HexNumber其中包含两个空格标志。)
参数 provider
是一个 IFormatProvider 实现,例如 NumberFormatInfo 或 CultureInfo 对象。 参数 provider
提供分析中使用的特定于区域性的信息。 如果 provider
为 null
, NumberFormatInfo 则使用当前区域性的 。
另请参阅
适用于
Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider)
- Source:
- Int64.cs
- Source:
- Int64.cs
- Source:
- Int64.cs
将指定样式和区域性特定格式的数字的范围表示形式转换为其等效的 64 位带符号整数。
public static long Parse (ReadOnlySpan<char> s, System.Globalization.NumberStyles style = System.Globalization.NumberStyles.Integer, IFormatProvider? provider = default);
public static long Parse (ReadOnlySpan<char> s, System.Globalization.NumberStyles style = System.Globalization.NumberStyles.Integer, IFormatProvider provider = default);
static member Parse : ReadOnlySpan<char> * System.Globalization.NumberStyles * IFormatProvider -> int64
Public Shared Function Parse (s As ReadOnlySpan(Of Char), Optional style As NumberStyles = System.Globalization.NumberStyles.Integer, Optional provider As IFormatProvider = Nothing) As Long
参数
- s
- ReadOnlySpan<Char>
一个范围,包含表示要转换的数字的字符。
- style
- NumberStyles
枚举值的按位组合,用于指示可出现在 s
中的样式元素。 要指定的一个典型值为 Integer。
- provider
- IFormatProvider
一个 IFormatProvider,它提供有关 s
的区域性特定格式设置信息。
返回
与 s
中指定的数字等效的 64 位带符号整数。
实现
适用于
Parse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider)
- Source:
- Int64.cs
- Source:
- Int64.cs
将 UTF-8 字符范围分析为值。
public static long Parse (ReadOnlySpan<byte> utf8Text, System.Globalization.NumberStyles style = System.Globalization.NumberStyles.Integer, IFormatProvider? provider = default);
static member Parse : ReadOnlySpan<byte> * System.Globalization.NumberStyles * IFormatProvider -> int64
Public Shared Function Parse (utf8Text As ReadOnlySpan(Of Byte), Optional style As NumberStyles = System.Globalization.NumberStyles.Integer, Optional provider As IFormatProvider = Nothing) As Long
参数
- utf8Text
- ReadOnlySpan<Byte>
要分析的 UTF-8 字符范围。
- style
- NumberStyles
可以存在于 中的 utf8Text
数字样式的按位组合。
- provider
- IFormatProvider
一个对象,提供有关 utf8Text
的区域性特定格式设置信息。
返回
分析 utf8Text
的结果。
实现
适用于
Parse(String, IFormatProvider)
- Source:
- Int64.cs
- Source:
- Int64.cs
- Source:
- Int64.cs
将指定的区域性特定格式的数字的字符串表示形式转换为它的等效 64 位有符号整数。
public:
static long Parse(System::String ^ s, IFormatProvider ^ provider);
public:
static long Parse(System::String ^ s, IFormatProvider ^ provider) = IParsable<long>::Parse;
public static long Parse (string s, IFormatProvider provider);
public static long Parse (string s, IFormatProvider? provider);
static member Parse : string * IFormatProvider -> int64
Public Shared Function Parse (s As String, provider As IFormatProvider) As Long
参数
- s
- String
包含要转换的数字的字符串。
- provider
- IFormatProvider
一个对象,提供有关 s
的区域性特定格式设置信息。
返回
与 s
中指定的数字等效的 64 位带符号整数。
实现
例外
s
为 null
。
s
的格式不正确。
s
表示小于 Int64.MinValue 或大于 Int64.MaxValue 的数字。
示例
以下示例是 Web 窗体的按钮单击事件处理程序。 它使用 属性返回的 HttpRequest.UserLanguages 数组来确定用户的区域设置。 然后,它实例化与 CultureInfo 该区域设置对应的 对象。 NumberFormatInfo然后,将属于该CultureInfo对象的 对象传递给 方法,Parse(String, IFormatProvider)以将用户的输入转换为Int64值。
protected void OkToLong_Click(object sender, EventArgs e)
{
string locale;
long 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 = Int64.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 OkToLong_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles OkToLong.Click
Dim locale As String
Dim culture As CultureInfo
Dim number As Long
' 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 = Int64.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) 此重载通常用于将可通过各种方式格式化的文本转换为 Int64 值。 例如,它可用于将用户在 HTML 文本框中输入的文本转换为数值。
参数 s
包含以下形式的数字:
[ws][sign]digits[ws]
方括号中的项 ([ 和 ]) 是可选的,其他项如下所示。
ws 可选空格。
sign 可选符号。
digits 范围从 0 到 9 的数字序列。
参数 s
是使用 样式解释的 NumberStyles.Integer 。 除十进制数字外,仅允许前导空格和尾随空格以及前导符号。 若要显式定义中可以存在的 s
样式元素,请使用 Int64.Parse(String, NumberStyles, IFormatProvider) 方法。
参数 provider
是实现 IFormatProvider ,如 NumberFormatInfo 或 CultureInfo 对象。 参数 provider
提供有关 格式 s
的区域性特定信息。 如果 provider
为 null
, NumberFormatInfo 则使用当前区域性的 。
另请参阅
适用于
Parse(String)
- Source:
- Int64.cs
- Source:
- Int64.cs
- Source:
- Int64.cs
将数字的字符串表示形式转换为它的等效 64 位有符号整数。
public:
static long Parse(System::String ^ s);
public static long Parse (string s);
static member Parse : string -> int64
Public Shared Function Parse (s As String) As Long
参数
- s
- String
包含要转换的数字的字符串。
返回
与 s
中包含的数字等效的 64 位有符号整数。
例外
s
为 null
。
s
的格式不正确。
s
表示小于 Int64.MinValue 或大于 Int64.MaxValue 的数字。
示例
下面的示例演示如何使用 Int64.Parse(String) 方法将字符串值转换为 64 位带符号整数值。 然后,它显示生成的长整型值。
using System;
public class ParseInt64
{
public static void Main()
{
Convert(" 179042 ");
Convert(" -2041326 ");
Convert(" +8091522 ");
Convert(" 1064.0 ");
Convert(" 178.3");
Convert(String.Empty);
Convert(((decimal) Int64.MaxValue) + 1.ToString());
}
private static void Convert(string value)
{
try
{
long number = Int64.Parse(value);
Console.WriteLine("Converted '{0}' to {1}.", value, number);
}
catch (FormatException)
{
Console.WriteLine("Unable to convert '{0}'.", value);
}
catch (OverflowException)
{
Console.WriteLine("'{0}' is out of range.", value);
}
}
}
// This example displays the following output to the console:
// Converted ' 179042 ' to 179042.
// Converted ' -2041326 ' to -2041326.
// Converted ' +8091522 ' to 8091522.
// Unable to convert ' 1064.0 '.
// Unable to convert ' 178.3'.
// Unable to convert ''.
// '92233720368547758071' is out of range.
open System
let convert value =
try
let number = Int64.Parse value
printfn $"Converted '{value}' to {number}."
with
| :? FormatException ->
printfn $"Unable to convert '{value}'."
| :? OverflowException ->
printfn $"'{value}' is out of range."
convert " 179042 "
convert " -2041326 "
convert " +8091522 "
convert " 1064.0 "
convert " 178.3"
convert String.Empty
decimal Int64.MaxValue + 1M
|> string
|> convert
// This example displays the following output to the console:
// Converted ' 179042 ' to 179042.
// Converted ' -2041326 ' to -2041326.
// Converted ' +8091522 ' to 8091522.
// Unable to convert ' 1064.0 '.
// Unable to convert ' 178.3'.
// Unable to convert ''.
// '92233720368547758071' is out of range.
Module ParseInt64
Public Sub Main()
Convert(" 179032 ")
Convert(" -2041326 ")
Convert(" +8091522 ")
Convert(" 1064.0 ")
Convert(" 178.3")
Convert(String.Empty)
Convert((CDec(Int64.MaxValue) + 1).ToString())
End Sub
Private Sub Convert(value As String)
Try
Dim number As Long = Int64.Parse(value)
Console.WriteLine("Converted '{0}' to {1}.", value, number)
Catch e As FormatException
Console.WriteLine("Unable to convert '{0}'.", value)
Catch e As OverflowException
Console.WriteLine("'{0}' is out of range.", value)
End Try
End Sub
End Module
' This example displays the following output to the console:
' Converted ' 179032 ' to 179032.
' Converted ' -2041326 ' to -2041326.
' Converted ' +8091522 ' to 8091522.
' Unable to convert ' 1064.0 '.
' Unable to convert ' 178.3'.
' Unable to convert ''.
' '9223372036854775808' is out of range.
注解
参数 s
包含以下形式的数字:
[ws][sign]digits[ws]
方括号 ([ and ]) 中的元素是可选的。 下表对每个元素进行了描述。
元素 | 说明 |
---|---|
ws | 可选空格。 |
签名 | 可选符号。 |
位数 | 从 0 到 9 的数字序列。 |
参数 s
是使用 样式解释的 NumberStyles.Integer 。 除十进制数字外,仅允许前导空格和尾随空格以及前导符号。 若要显式定义中可存在的 s
样式元素,请使用 Int64.Parse(String, NumberStyles) 或 Int64.Parse(String, NumberStyles, IFormatProvider) 方法。
参数 s
是使用针对当前系统区域性初始化的 对象中的 NumberFormatInfo 格式设置信息进行分析的。 若要使用其他区域性的格式设置信息分析字符串,请使用 Int64.Parse(String, NumberStyles, IFormatProvider) 方法。
另请参阅
适用于
Parse(ReadOnlySpan<Char>, IFormatProvider)
- Source:
- Int64.cs
- Source:
- Int64.cs
- Source:
- Int64.cs
将字符范围分析为值。
public:
static long Parse(ReadOnlySpan<char> s, IFormatProvider ^ provider) = ISpanParsable<long>::Parse;
public static long Parse (ReadOnlySpan<char> s, IFormatProvider? provider);
static member Parse : ReadOnlySpan<char> * IFormatProvider -> int64
Public Shared Function Parse (s As ReadOnlySpan(Of Char), provider As IFormatProvider) As Long
参数
- s
- ReadOnlySpan<Char>
要分析的字符范围。
- provider
- IFormatProvider
一个对象,提供有关 s
的区域性特定格式设置信息。
返回
分析 s
的结果。
实现
适用于
Parse(ReadOnlySpan<Byte>, IFormatProvider)
- Source:
- Int64.cs
- Source:
- Int64.cs
将 UTF-8 字符范围分析为值。
public:
static long Parse(ReadOnlySpan<System::Byte> utf8Text, IFormatProvider ^ provider) = IUtf8SpanParsable<long>::Parse;
public static long Parse (ReadOnlySpan<byte> utf8Text, IFormatProvider? provider);
static member Parse : ReadOnlySpan<byte> * IFormatProvider -> int64
Public Shared Function Parse (utf8Text As ReadOnlySpan(Of Byte), provider As IFormatProvider) As Long
参数
- utf8Text
- ReadOnlySpan<Byte>
要分析的 UTF-8 字符范围。
- provider
- IFormatProvider
一个对象,提供有关 utf8Text
的区域性特定格式设置信息。
返回
分析 utf8Text
的结果。
实现
适用于
Parse(String, NumberStyles)
- Source:
- Int64.cs
- Source:
- Int64.cs
- Source:
- Int64.cs
将指定样式的数字的字符串表示形式转换为它的等效 64 位有符号整数。
public:
static long Parse(System::String ^ s, System::Globalization::NumberStyles style);
public static long Parse (string s, System.Globalization.NumberStyles style);
static member Parse : string * System.Globalization.NumberStyles -> int64
Public Shared Function Parse (s As String, style As NumberStyles) As Long
参数
- s
- String
包含要转换的数字的字符串。
- style
- NumberStyles
NumberStyles 值的按位组合,指示 s
的允许格式。 要指定的一个典型值为 Integer。
返回
与 s
中指定的数字等效的 64 位带符号整数。
例外
s
为 null
。
s
的格式不符合 style
。
示例
以下示例使用 Int64.Parse(String, NumberStyles) 方法分析多个 Int64 值的字符串表示形式。 本示例的当前区域性为 en-US。
using System;
using System.Globalization;
public class ParseInt32
{
public static void Main()
{
Convert("104.0", NumberStyles.AllowDecimalPoint);
Convert("104.9", NumberStyles.AllowDecimalPoint);
Convert (" 106034", NumberStyles.None);
Convert(" $17,198,064.42", NumberStyles.AllowCurrencySymbol |
NumberStyles.Number);
Convert(" $17,198,064.00", NumberStyles.AllowCurrencySymbol |
NumberStyles.Number);
Convert("103E06", NumberStyles.AllowExponent);
Convert("1200E-02", NumberStyles.AllowExponent);
Convert("1200E-03", NumberStyles.AllowExponent);
Convert("-1,345,791", NumberStyles.AllowThousands);
Convert("(1,345,791)", NumberStyles.AllowThousands |
NumberStyles.AllowParentheses);
Convert("FFCA00A0", NumberStyles.HexNumber);
Convert("0xFFCA00A0", NumberStyles.HexNumber);
}
private static void Convert(string value, NumberStyles style)
{
try
{
long number = Int64.Parse(value, style);
Console.WriteLine("Converted '{0}' to {1}.", value, number);
}
catch (FormatException)
{
Console.WriteLine("Unable to convert '{0}'.", value);
}
catch (OverflowException)
{
Console.WriteLine("'{0}' is out of range of the Int64 type.", value);
}
}
}
// The example displays the following output to the console:
// Converted '104.0' to 104.
// '104.9' is out of range of the Int64 type.
// Unable to convert ' 106034'.
// ' $17,198,064.42' is out of range of the Int64 type.
// Converted ' $17,198,064.00' to 17198064.
// Converted '103E06' to 103000000.
// Converted '1200E-02' to 12.
// '1200E-03' is out of range of the Int64 type.
// Unable to convert '-1,345,791'.
// Converted '(1,345,791)' to -1345791.
// Converted 'FFCA00A0' to 4291428512.
// Unable to convert '0xFFCA00A0'.
open System
open System.Globalization
let convert value (style: NumberStyles) =
try
let number = Int64.Parse(value, style)
printfn $"converted '{value}' to {number}."
with
| :? FormatException ->
printfn $"Unable to convert '{value}'."
| :? OverflowException ->
printfn $"'{value}' is out of range of the Int64 type."
convert "104.0" NumberStyles.AllowDecimalPoint
convert "104.9" NumberStyles.AllowDecimalPoint
convert " 106034" NumberStyles.None
convert " $17,198,064.42" (NumberStyles.AllowCurrencySymbol ||| NumberStyles.Number)
convert " $17,198,064.00" (NumberStyles.AllowCurrencySymbol ||| NumberStyles.Number)
convert "103E06" NumberStyles.AllowExponent
convert "1200E-02" NumberStyles.AllowExponent
convert "1200E-03" NumberStyles.AllowExponent
convert "-1,345,791" NumberStyles.AllowThousands
convert "(1,345,791)" (NumberStyles.AllowThousands ||| NumberStyles.AllowParentheses)
convert "FFCA00A0" NumberStyles.HexNumber
convert "0xFFCA00A0" NumberStyles.HexNumber
// The example displays the following output to the console:
// converted '104.0' to 104.
// '104.9' is out of range of the Int64 type.
// Unable to convert ' 106034'.
// ' $17,198,064.42' is out of range of the Int64 type.
// converted ' $17,198,064.00' to 17198064.
// converted '103E06' to 103000000.
// converted '1200E-02' to 12.
// '1200E-03' is out of range of the Int64 type.
// Unable to convert '-1,345,791'.
// converted '(1,345,791)' to -1345791.
// converted 'FFCA00A0' to 4291428512.
// Unable to convert '0xFFCA00A0'.
Imports System.Globalization
Module ParseInt64
Public Sub Main()
Convert("104.0", NumberStyles.AllowDecimalPoint)
Convert("104.9", NumberStyles.AllowDecimalPoint)
Convert (" 106034", NumberStyles.None)
Convert(" $17,198,064.42", NumberStyles.AllowCurrencySymbol Or _
NumberStyles.Number)
Convert(" $17,198,064.00", NumberStyles.AllowCurrencySymbol Or _
NumberStyles.Number)
Convert("103E06", NumberStyles.AllowExponent)
Convert("1200E-02", NumberStyles.AllowExponent)
Convert("1200E-03", NumberStyles.AllowExponent)
Convert("-1,345,791", NumberStyles.AllowThousands)
Convert("(1,345,791)", NumberStyles.AllowThousands Or _
NumberStyles.AllowParentheses)
Convert("FFCA00A0", NumberStyles.HexNumber)
Convert("0xFFCA00A0", NumberStyles.HexNumber)
End Sub
Private Sub Convert(value As String, style As NumberStyles)
Try
Dim number As Long = Int64.Parse(value, style)
Console.WriteLine("Converted '{0}' to {1}.", value, number)
Catch e As FormatException
Console.WriteLine("Unable to convert '{0}'.", value)
Catch e As OverflowException
Console.WriteLine("'{0}' is out of range of the Int64 type.", value)
End Try
End Sub
End Module
' The example displays the following output to the console:
' Converted '104.0' to 104.
' '104.9' is out of range of the Int64 type.
' Unable to convert ' 106034'.
' ' $17,198,064.42' is out of range of the Int64 type.
' Converted ' $17,198,064.00' to 17198064.
' Converted '103E06' to 103000000.
' Converted '1200E-02' to 12.
' '1200E-03' is out of range of the Int64 type.
' Unable to convert '-1,345,791'.
' Converted '(1,345,791)' to -1345791.
' Converted 'FFCA00A0' to 4291428512.
' Unable to convert '0xFFCA00A0'.
注解
参数 style
定义样式元素 (,如空格、正号或负号符号或千位分隔符) ,这些符号在 参数中 s
允许分析操作成功。 它必须是 枚举中的位标志 NumberStyles 的组合。 根据 的值 style
, s
参数可能包含以下元素:
[ws][$][sign][digits,]digits[.fractional_digits][e[sign]exponential_digits][ws]
或者,如果 style
包括 AllowHexSpecifier:
[ws]hexdigits[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 括号来指示负值。 |
位数 fractional_digits exponential_digits |
从 0 到 9 的数字序列。 对于 fractional_digits,只有数字 0 有效。 |
, | 区域性特定的千位分隔符。 如果style 包含 NumberStyles.AllowThousands 标志,则当前区域性的千位分隔符可以出现在 中s 。 |
. | 区域性特定的小数点符号。 如果style 包含 NumberStyles.AllowDecimalPoint 标志,则当前区域性的小数点符号可以出现在 中s 。 只有数字 0 可以显示为分析操作成功的小数位数;如果 fractional_digits 包含任何其他数字, OverflowException 则引发 。 |
e | “e”或“E”字符,指示该值以指数表示法表示。 如果style 包含 标志,则 s 参数可以表示指数表示法中的NumberStyles.AllowExponent数字。 |
hexdigits | 从 0 到 f 或 0 到 F 的十六进制数字序列。 |
注意
分析操作将忽略中 s
任何 (U+0000) 字符的终止 NUL,而不考虑 参数的值 style
。
仅包含数字的字符串 (对应于 NumberStyles.None 样式) 如果位于类型范围内 Int64 ,则始终会成功分析。 其余 NumberStyles 大多数成员控制可能为 但不需要存在于输入字符串中的元素。 下表指示各个 NumberStyles 成员如何影响 中可能存在 s
的元素。
NumberStyles 值 | 除了数字外,还允许 在 中 |
---|---|
None | 仅限 digits 元素。 |
AllowDecimalPoint | 小数点 ( . ) 和 小数位数 元素。 |
AllowExponent | 参数 s 还可以使用指数表示法。 如果 s 以指数表示法表示数字,则生成的数值不能包含任何非零小数位数。 |
AllowLeadingWhite | 开头的 s ws 元素。 |
AllowTrailingWhite | 位于 末尾的 s ws 元素。 |
AllowLeadingSign | 开头的s 符号元素。 |
AllowTrailingSign | 末尾的s 符号元素。 |
AllowParentheses | 用括号括住数值的 符号 元素。 |
AllowThousands | 千位分隔符 ( , ) 元素。 |
AllowCurrencySymbol | $ 元素。 |
Currency | 全部。 参数 s 不能表示十六进制数或指数表示法中的数字。 |
Float | 开头或结尾处的 s ws元素,符号位于 开头,s 小数点 ( .) 符号。 参数 s 还可以使用指数表示法。 |
Number | ws、符号、千位分隔符 ( 、) 和小数点 ( . ) 元素。 |
Any | 所有样式(除外 s )都不能表示十六进制数。 |
如果使用标志 NumberStyles.AllowHexSpecifier , s
必须是不带前缀的十六进制值。 例如,“C9AF3”分析成功,但“0xC9AF3”则不成功。 唯一可以与 s
参数组合的其他标志是 NumberStyles.AllowLeadingWhite 和 NumberStyles.AllowTrailingWhite。 (枚举 NumberStyles 包含复合数字样式 NumberStyles.HexNumber,其中包含空格标志 )
参数 s
是使用针对当前系统区域性初始化的 对象中的 NumberFormatInfo 格式设置信息进行分析的。 若要指定其格式设置信息用于分析操作的区域性,请 Int64.Parse(String, NumberStyles, IFormatProvider) 调用 重载。
另请参阅
适用于
反馈
https://aka.ms/ContentUserFeedback。
即将发布:在整个 2024 年,我们将逐步淘汰作为内容反馈机制的“GitHub 问题”,并将其取代为新的反馈系统。 有关详细信息,请参阅:提交和查看相关反馈