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)
指定したスタイルおよびカルチャに固有の書式による数値の文字列形式を、それと等価の 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
s
に関するカルチャに固有の書式設定情報を提供する IFormatProvider。
戻り値
s
で指定した数値と等しい 64 ビット符号付き整数。
実装
例外
s
が null
です。
s
の形式が style
に準拠していません。
s
は、Int64.MinValue 未満または Int64.MaxValue より大きい数値を表します。
- または -
style
は小数の桁をサポートしていますが、s
に 0 以外の小数の桁が含まれています。
例
次の例では、さまざまな 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]
または、 が含まれているAllowHexSpecifier場合style
は 、
[ws]hexdigits[ws]
角かっこ ([ および ]) 内の要素は省略可能です。 次の表は、それぞれの要素の説明です。
要素 | 説明 |
---|---|
ws | オプションの空白。 空白は、 フラグを含む場合は のs 先頭に表示され、フラグが含NumberStyles.AllowLeadingWhiteまれている場合style は のs 末尾にNumberStyles.AllowTrailingWhite表示style されます。 |
$ | カルチャ固有の通貨記号。 文字列内での位置は、 パラメーターの NumberFormatInfo.CurrencyPositivePatternNumberFormatInfo メソッドによって返されるオブジェクトの provider プロパティによってGetFormat定義されます。 に フラグが含まれている場合style は、通貨記号を にs NumberStyles.AllowCurrencySymbol表示できます。 |
sign | 省略可能な記号。 記号は、 フラグが含まれている場合は のs 先頭に表示され、フラグがNumberStyles.AllowLeadingSign含まれている場合style は のs 末尾にNumberStyles.AllowTrailingSign表示style されます。 に フラグが含まれている場合style は、かっこを使用s して負の値をNumberStyles.AllowParentheses示すことができます。 |
数値 fractional_digits exponential_digits |
0 から 9 までの数字のシーケンス。 |
, | カルチャ固有の桁区切り記号。 でprovider 指定されたカルチャの桁区切り記号は、 に フラグが含まれている場合style にs NumberStyles.AllowThousands表示できます。 |
. | カルチャ固有の小数点記号。 でprovider 指定されたカルチャの小数点記号は、 に フラグが含まれている場合style にs NumberStyles.AllowDecimalPoint表示できます。解析操作を成功させるために小数部の数字として表示できるのは、数字 0 のみです。 fractional_digits に他の数字が含まれている場合は、 OverflowException がスローされます。 |
e | 値が指数表記で表されることを示す 'e' または 'E' 文字。 フラグが含まれている場合style 、パラメーターはs 指数表記で数値をNumberStyles.AllowExponent表すことができます。 |
hexdigits | 0 から f、または 0 から F までの 16 進数のシーケンス。 |
注意
の終端の NUL (U+0000) 文字 s
は、引数の style
値に関係なく、解析操作では無視されます。
10 進数のみの文字列 (スタイルに NumberStyles.None 対応) は、型の Int64 範囲内にある場合は常に正常に解析されます。 残りの NumberStyles メンバーのほとんどは、この入力文字列に存在する必要がない要素を制御します。 次の表は、 に存在する可能性がある要素に対する個々 NumberStyles のメンバーの s
影響を示しています。
非複合 NumberStyles 値 | 数字に加えて、 で許可される要素 |
---|---|
NumberStyles.None | 10 進数のみ。 |
NumberStyles.AllowDecimalPoint | 小数点 ( . ) および 小数部の要素 。 ただし、 小数部の数字 は 1 つ以上の 0 桁のみで構成する必要があります。または、 OverflowException がスローされます。 |
NumberStyles.AllowExponent | パラメーターでは s 、指数表記を使用することもできます。 |
NumberStyles.AllowLeadingWhite | の先頭s にある ws 要素。 |
NumberStyles.AllowTrailingWhite | の末尾s にある ws 要素。 |
NumberStyles.AllowLeadingSign | 記号は 数字の前に表示できます。 |
NumberStyles.AllowTrailingSign | 数字 の後に記号が表示される場合があります。 |
NumberStyles.AllowParentheses | 数値を囲むかっこの形式の sign 要素。 |
NumberStyles.AllowThousands | 桁区切り記号 ( 、 ) 要素。 |
NumberStyles.AllowCurrencySymbol | $ 要素。 |
フラグを使用する NumberStyles.AllowHexSpecifier 場合は、 s
プレフィックスのない 16 進値を指定する必要があります。 たとえば、"C9AF3" は正常に解析されますが、"0xC9AF3" では解析されません。 に存在できるその他の style
フラグは NumberStyles.AllowLeadingWhite と NumberStyles.AllowTrailingWhiteのみです。 (列挙型には NumberStyles 、 NumberStyles.HexNumber両方の空白フラグを含む複合数値スタイル があります)。
パラメーターはprovider
、 IFormatProvider オブジェクトや CultureInfo オブジェクトなどのNumberFormatInfo実装です。 パラメーターは provider
、解析で使用されるカルチャ固有の情報を提供します。 が のnull
場合provider
は、NumberFormatInfo現在のカルチャの が使用されます。
こちらもご覧ください
適用対象
Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider)
指定したスタイルおよびカルチャに固有の書式による数値のスパン表現を、等価の 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
s
に関するカルチャに固有の書式設定情報を提供する IFormatProvider。
戻り値
s
で指定した数値と等しい 64 ビット符号付き整数。
実装
適用対象
Parse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider)
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)
指定したカルチャに固有の書式による数値の文字列形式を、それと等価な 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 解釈されます。 10 進数に加えて、先頭と末尾のスペースと先頭の記号のみを使用できます。 に存在できるスタイル要素を明示的に s
定義するには、 メソッドを使用します Int64.Parse(String, NumberStyles, IFormatProvider) 。
パラメーターはprovider
、 IFormatProvider や CultureInfo オブジェクトなどのNumberFormatInfo実装です。 パラメーターは provider
、 の形式に関するカルチャ固有の s
情報を提供します。 が のnull
場合provider
は、NumberFormatInfo現在のカルチャの が使用されます。
こちらもご覧ください
適用対象
Parse(String)
数値の文字列形式を、それと等価な 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 より大きい数値を表します。
例
次の例では、 メソッドを使用して文字列値を 64 ビット符号付き整数値に変換する方法を Int64.Parse(String) 示します。 その後、結果の長整数型 (long) の整数値が表示されます。
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]
角かっこ ([ および ]) 内の要素は省略可能です。 次の表は、それぞれの要素の説明です。
要素 | 説明 |
---|---|
ws | オプションの空白。 |
sign | 省略可能な記号。 |
数値 | 0 から 9 までの数字のシーケンス。 |
パラメーターは s
、 スタイルを使用して NumberStyles.Integer 解釈されます。 10 進数に加えて、先頭と末尾のスペースと先頭の記号のみを使用できます。 に存在できるスタイル要素を明示的に s
定義するには、 Int64.Parse(String, NumberStyles) メソッドまたは メソッドを Int64.Parse(String, NumberStyles, IFormatProvider) 使用します。
パラメーターは s
、現在のシステム カルチャ用に初期化された オブジェクトの NumberFormatInfo 書式設定情報を使用して解析されます。 他のカルチャの書式設定情報を使用して文字列を解析するには、 メソッドを使用します Int64.Parse(String, NumberStyles, IFormatProvider) 。
こちらもご覧ください
適用対象
Parse(ReadOnlySpan<Char>, IFormatProvider)
文字のスパンを値に解析します。
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)
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)
指定したスタイルの数値の文字列形式を、それと等価の 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
に準拠していません。
s
は、 Int64.MinValue より小さい数値または Int64.MaxValue より大きい数値を表します。
- または -
style
は小数の桁数をサポートしていますが、s
に 0 以外の小数の桁数が含まれています。
例
次の例では、 メソッドを 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]
または、 が含まれているAllowHexSpecifier場合style
は 。
[ws]hexdigits[ws]
角かっこ ([ および ]) 内の要素は省略可能です。 次の表は、それぞれの要素の説明です。
要素 | 説明 |
---|---|
ws | オプションの空白。 空白は、 フラグを含む場合は のs 先頭に表示でき、フラグが含NumberStyles.AllowLeadingWhiteまれている場合style は のs 末尾にNumberStyles.AllowTrailingWhite表示style できます。 |
$ | カルチャ固有の通貨記号。 文字列内での位置は、現在のカルチャの NumberFormatInfo.CurrencyNegativePattern プロパティと NumberFormatInfo.CurrencyPositivePattern プロパティによって定義されます。 フラグが含まれている場合style は、現在のカルチャの通貨記号を にs NumberStyles.AllowCurrencySymbol表示できます。 |
sign | 省略可能な記号。 記号は、 フラグを含む場合は のs 先頭に表示でき、フラグが含NumberStyles.AllowLeadingSignまれている場合style は のs 末尾にNumberStyles.AllowTrailingSign表示style できます。 に フラグが含まれている場合style 、かっこを使用s して負の値をNumberStyles.AllowParentheses示すことができます。 |
数値 fractional_digits exponential_digits |
0 ~ 9 の数字のシーケンス。 fractional_digitsの場合、数字 0 のみが有効です。 |
, | カルチャ固有の桁区切り記号。 現在のカルチャの桁区切り記号は、 フラグが含まれている場合style にs NumberStyles.AllowThousandsに表示できます。 |
. | カルチャ固有の小数点記号。 現在のカルチャの小数点記号は、 フラグが含まれている場合style にNumberStyles.AllowDecimalPoints に表示できます。 解析操作を成功させるために小数部の数字として表示できるのは、数字 0 だけです。fractional_digitsに他 の 数字が含まれている場合は、 OverflowException がスローされます。 |
e | 値が指数表記で表されることを示す 'e' または 'E' 文字。 フラグが含まれている場合style 、パラメーターはs 指数表記で数値をNumberStyles.AllowExponent表すことができます。 |
hexdigits | 0 から f、または 0 から F までの 16 進数のシーケンス。 |
注意
の終端 NUL (U+0000) 文字 s
は、引数の style
値に関係なく、解析操作では無視されます。
数字のみを含む文字列 (スタイルに NumberStyles.None 対応) は、型の Int64 範囲内にある場合は常に正常に解析されます。 残りの NumberStyles メンバーのほとんどは、入力文字列に存在する必要がない要素を制御します。 次の表は、 にs
存在する可能性がある要素に個々NumberStylesのメンバーがどのように影響するかを示しています。
NumberStyles 値 | 数字に加えて、 で許可される要素 |
---|---|
None | digits 要素のみ。 |
AllowDecimalPoint | 小数点 ( . ) と 小数部の要素 。 |
AllowExponent | パラメーターでは s 指数表記を使用することもできます。 指数表記で数値を表す場合 s 、結果の数値に 0 以外の小数部を含めることはできません。 |
AllowLeadingWhite | の先頭s にある ws 要素。 |
AllowTrailingWhite | の末尾s にある ws 要素。 |
AllowLeadingSign | の先頭s にある sign 要素。 |
AllowTrailingSign | の末尾s にある sign 要素。 |
AllowParentheses | 数値を囲むかっこの形式の 符号 要素。 |
AllowThousands | 桁区切り記号 ( 、 ) 要素。 |
AllowCurrencySymbol | $ 要素。 |
Currency | すべて。 パラメーターは s 、指数表記で 16 進数または数値を表すことができません。 |
Float | の先頭または末尾にある s ws 要素、の先頭にs 記号を付け、小数点 ( . ) 記号を指定します。 パラメーターでは s 指数表記を使用することもできます。 |
Number | ws、sign、thousands separator ( 、)、および decimal point ( . ) 要素。 |
Any | を除く s すべてのスタイルは、16 進数を表すことはできません。 |
フラグを使用する NumberStyles.AllowHexSpecifier 場合は、 s
プレフィックスのない 16 進数の値にする必要があります。 たとえば、"C9AF3" は正常に解析されますが、"0xC9AF3" は解析されません。 パラメーターと組み合わせることができる他のフラグは NumberStyles.AllowLeadingWhite と s
NumberStyles.AllowTrailingWhiteのみです。 (列挙には NumberStyles 、 NumberStyles.HexNumber両方の空白フラグを含む複合数値スタイル が含まれています)。
パラメーターは s
、現在のシステム カルチャ用に初期化された オブジェクトの NumberFormatInfo 書式設定情報を使用して解析されます。 解析操作に書式設定情報を使用するカルチャを指定するには、 オーバーロードを Int64.Parse(String, NumberStyles, IFormatProvider) 呼び出します。
こちらもご覧ください
適用対象
フィードバック
フィードバックの送信と表示