Single.TryParse メソッド

定義

数値の文字列形式を、等価の単精度浮動小数点数に変換します。 戻り値は、変換が成功したか失敗したかを示します。

オーバーロード

TryParse(String, IFormatProvider, Single)

文字列を値に解析しようとします。

TryParse(ReadOnlySpan<Char>, Single)

文字スパン内の数値の文字列形式を、等価の単精度浮動小数点数に変換します。 戻り値は、変換が成功したか失敗したかを示します。

TryParse(String, Single)

数値の文字列形式を、等価の単精度浮動小数点数に変換します。 戻り値は、変換が成功したか失敗したかを示します。

TryParse(ReadOnlySpan<Byte>, IFormatProvider, Single)

UTF-8 文字のスパンを値に解析しようとします。

TryParse(ReadOnlySpan<Char>, IFormatProvider, Single)

文字のスパンを値に解析しようとします。

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

UTF-8 文字のスパンを値に解析しようとします。

TryParse(ReadOnlySpan<Byte>, Single)

数値の文字列形式を含む UTF-8 文字スパンを、等価の単精度浮動小数点数に変換しようとします。

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

指定したスタイルおよびカルチャ固有の形式の数値のスパン表現を、等価の単精度浮動小数点数に変換します。 戻り値は、変換が成功したか失敗したかを示します。

TryParse(String, NumberStyles, IFormatProvider, Single)

指定したスタイルおよびカルチャ固有の形式の数値の文字列形式を、等価の単精度浮動小数点数に変換します。 戻り値は、変換が成功したか失敗したかを示します。

注釈

.NET Core 3.0 以降では、IEEE 754 仕様で必要に応じて、大きすぎる値は PositiveInfinity または NegativeInfinity に丸められます。 以前のバージョン (.NET Framework を含む) では、大きすぎる値を解析するとエラーが発生しました。

TryParse(String, IFormatProvider, Single)

ソース:
Single.cs
ソース:
Single.cs
ソース:
Single.cs

文字列を値に解析しようとします。

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

パラメーター

s
String

解析する文字列。

provider
IFormatProvider

sに関するカルチャ固有の書式設定情報を提供するオブジェクト。

result
Single

このメソッドから制御が戻るときに、正常に s 解析された結果または失敗した場合の未定義の値が格納されます。

戻り値

s が正常に解析されたかどうかを true します。それ以外の場合は、falseします。

適用対象

TryParse(ReadOnlySpan<Char>, Single)

ソース:
Single.cs
ソース:
Single.cs
ソース:
Single.cs

文字スパン内の数値の文字列形式を、等価の単精度浮動小数点数に変換します。 戻り値は、変換が成功したか失敗したかを示します。

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

パラメーター

s
ReadOnlySpan<Char>

>変換する数値の文字列形式を含む文字スパンです。

result
Single

このメソッドから制御が戻るときに、変換に成功した場合は s パラメーターに相当する単精度浮動小数点数を格納し、変換に失敗した場合は 0 を格納します。 s パラメーターが null または空であるか、有効な形式の数値でない場合、変換は失敗します。 が Single.MinValueより小さい有効な数値である場合、 が Single.MaxValueより大きい有効な数値 場合、。 このパラメーターは初期化されていない状態で渡されます。result で最初に指定された値は上書きされます。

戻り値

s が正常に変換されたかどうかを true します。それ以外の場合は、falseします。

注釈

.NET Core 3.0 以降では、IEEE 754 仕様で必要に応じて、大きすぎる値は PositiveInfinity または NegativeInfinity に丸められます。 以前のバージョン (.NET Framework を含む) では、大きすぎる値を解析するとエラーが発生しました。

適用対象

TryParse(String, Single)

ソース:
Single.cs
ソース:
Single.cs
ソース:
Single.cs

数値の文字列形式を、等価の単精度浮動小数点数に変換します。 戻り値は、変換が成功したか失敗したかを示します。

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

パラメーター

s
String

変換する数値を表す文字列。

result
Single

このメソッドから制御が戻るときに、変換に成功した場合は、sに含まれる数値または記号に相当する単精度浮動小数点数を格納し、変換に失敗した場合は 0 を格納します。 s パラメーターが null または Empty であるか、有効な形式の数値でない場合、変換は失敗します。 また、.NET Framework および .NET Core 2.2 以前のバージョンでも、 が Single.MinValue より小さいか、Single.MaxValueより大きい数値を表している場合に失敗します。 このパラメーターは初期化されていない状態で渡されます。result で最初に指定された値は上書きされます。

戻り値

s が正常に変換されたかどうかを true します。それ以外の場合は、falseします。

次の例では、TryParse(String, Single) メソッドを使用して、数値の文字列形式を Single 値に変換します。 en-US が現在のカルチャであることを前提としています。

string value;
float number;

// Parse a floating-point value with a thousands separator.
value = "1,643.57";
if (Single.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 (Single.TryParse(value, out number))
   Console.WriteLine(number);
else
   Console.WriteLine("Unable to parse '{0}'.", value);

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

// Parse a negative integer value.
value = "-168934617882109132";
if (Single.TryParse(value, out number))
   Console.WriteLine(number);
else
   Console.WriteLine("Unable to parse '{0}'.", value);
// The example displays the following output:
//       1643.57
//       Unable to parse '$1,643.57'.
//       -164300
//       -1.689346E+17
// Parse a floating-point value with a thousands separator.
let value = "1,643.57"
match Single.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 Single.TryParse value with
| true, number ->
    printfn $"{number}"
| _ ->
    printfn $"Unable to parse '{value}'."

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

// Parse a negative integer value.
let value = "-168934617882109132"
match Single.TryParse value with
| true, number ->
    printfn $"{number}"
| _ ->
    printfn $"Unable to parse '{value}'."
// The example displays the following output:
//       1643.57
//       Unable to parse '$1,643.57'.
//       -164300
//       -1.689346E+17
Dim value As String
Dim number As Single

' Parse a floating-point value with a thousands separator.
value = "1,643.57"
If Single.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 Single.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 Single.TryParse(value, number)
   Console.WriteLine(number)
Else
   Console.WriteLine("Unable to parse '{0}'.", value)
End If

' Parse a negative integer number.
value = "-168934617882109132"
If Single.TryParse(value, number)
   Console.WriteLine(number)
Else
   Console.WriteLine("Unable to parse '{0}'.", value)
End If
' The example displays the following output:
'       1643.57
'       Unable to parse '$1,643.57'.
'       -1643000
'       -1.689346E+17

注釈

.NET Core 3.0 以降では、IEEE 754 仕様で必要に応じて、大きすぎる値は PositiveInfinity または NegativeInfinity に丸められます。 以前のバージョン (.NET Framework を含む) では、大きすぎる値を解析するとエラーが発生しました。

このオーバーロードは、解析された数値を返す代わりに解析操作が成功したかどうかを示すブール値を返すことによって、Single.Parse(String) メソッドとは異なります。 これにより、s が無効であり、正常に解析できない場合に、例外処理を使用して FormatException をテストする必要がなくなります。

s パラメーターには、PositiveInfinitySymbolNegativeInfinitySymbolNaNSymbol (文字列比較では大文字と小文字が区別されます)、または形式の文字列を含めることができます。

[ws][sign][整数桁,]整数桁[.[fractional-digits]][e[sign]exponential-digits][ws]

角かっこ内の要素は省略可能です。 次の表では、各要素について説明します。

要素 形容
ws の 一連の空白文字。
sign 負符号または正符号記号。
整数桁の 数値の整数部分を指定する 0 から 9 までの一連の数値。 整数桁は、小数部が存在する場合は使用できません。
カルチャ固有のグループ区切り記号。
. カルチャ固有の小数点記号。
小数部の を する 数値の小数部を指定する 0 から 9 までの一連の数値。
E 指数 (指数) 表記を示す大文字または小文字の 'e' です。
指数桁 を する 指数を指定する 0 から 9 までの一連の数値。

s パラメーターは、NumberStyles.Float フラグと NumberStyles.AllowThousands フラグの組み合わせを使用して解釈されます。 つまり、空白と桁区切り記号は使用できますが、通貨記号は使用できません。 sに存在できる要素 (通貨記号、桁区切り記号、空白など) を明示的に定義するには、TryParse(String, NumberStyles, IFormatProvider, Single) メソッドのオーバーロードを使用します。

s パラメーターは、現在のシステム カルチャ用に初期化された NumberFormatInfo オブジェクトの書式設定情報を使用して解析されます。 詳細については、NumberFormatInfo.CurrentInfoを参照してください。 他の指定されたカルチャの書式設定情報を使用して文字列を解析するには、TryParse(String, NumberStyles, IFormatProvider, Single) メソッドのオーバーロードを使用します。

通常、Single.ToString メソッドを呼び出して作成された文字列を Single.TryParse メソッドに渡すと、元の Single 値が返されます。 ただし、精度が失われるため、値が等しくない可能性があります。

sSingle データ型の範囲外の場合、.NET Framework および .NET Core 2.2 以前のバージョンの false が返されます。 .NET Core 3.0 以降のバージョンでは、sSingle.MinValue 未満の場合は Single.NegativeInfinity を返し、sSingle.MaxValueより大きい場合は Single.PositiveInfinity

解析操作中に s パラメーターで区切り記号が検出され、該当する通貨または数値の小数点とグループの区切り記号が同じである場合、解析操作では、区切り記号がグループ区切り記号ではなく小数点であると見なされます。 区切り記号の詳細については、「CurrencyDecimalSeparatorNumberDecimalSeparatorCurrencyGroupSeparator、および NumberGroupSeparator」を参照してください。

こちらもご覧ください

適用対象

TryParse(ReadOnlySpan<Byte>, IFormatProvider, Single)

ソース:
Single.cs
ソース:
Single.cs

UTF-8 文字のスパンを値に解析しようとします。

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

パラメーター

utf8Text
ReadOnlySpan<Byte>

解析する UTF-8 文字のスパン。

provider
IFormatProvider

utf8Textに関するカルチャ固有の書式設定情報を提供するオブジェクト。

result
Single

戻り値には、utf8Text が正常に解析された結果、または失敗した場合に未定義の値が含まれます。

戻り値

utf8Text が正常に解析されたかどうかを true します。それ以外の場合は、falseします。

適用対象

TryParse(ReadOnlySpan<Char>, IFormatProvider, Single)

ソース:
Single.cs
ソース:
Single.cs
ソース:
Single.cs

文字のスパンを値に解析しようとします。

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

パラメーター

s
ReadOnlySpan<Char>

解析する文字のスパン。

provider
IFormatProvider

sに関するカルチャ固有の書式設定情報を提供するオブジェクト。

result
Single

このメソッドから制御が戻るときに、sが正常に解析された結果、または失敗した場合は未定義の値が格納されます。

戻り値

s が正常に解析されたかどうかを true します。それ以外の場合は、falseします。

適用対象

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

ソース:
Single.cs
ソース:
Single.cs

UTF-8 文字のスパンを値に解析しようとします。

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

パラメーター

utf8Text
ReadOnlySpan<Byte>

解析する UTF-8 文字のスパン。

style
NumberStyles

utf8Textに存在できる数値スタイルのビットごとの組み合わせ。

provider
IFormatProvider

utf8Textに関するカルチャ固有の書式設定情報を提供するオブジェクト。

result
Single

戻り値には、utf8Text が正常に解析された結果、または失敗した場合に未定義の値が含まれます。

戻り値

utf8Text が正常に解析されたかどうかを true します。それ以外の場合は、falseします。

適用対象

TryParse(ReadOnlySpan<Byte>, Single)

ソース:
Single.cs
ソース:
Single.cs

数値の文字列形式を含む UTF-8 文字スパンを、等価の単精度浮動小数点数に変換しようとします。

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

パラメーター

utf8Text
ReadOnlySpan<Byte>

変換する数値を含む読み取り専用 UTF-8 文字スパン。

result
Single

このメソッドから制御が戻るときに、変換に成功した場合は、utf8Text に含まれる数値または記号に相当する単精度浮動小数点数が格納されます。変換に失敗した場合は 0 が格納されます。 utf8TextEmpty されているか、有効な形式でない場合、変換は失敗します。 このパラメーターは初期化されていない状態で渡されます。結果で最初に指定された値は上書きされます。

戻り値

utf8Text が正常に変換されたかどうかを true します。それ以外の場合は、falseします。

適用対象

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

ソース:
Single.cs
ソース:
Single.cs
ソース:
Single.cs

指定したスタイルおよびカルチャ固有の形式の数値のスパン表現を、等価の単精度浮動小数点数に変換します。 戻り値は、変換が成功したか失敗したかを示します。

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

パラメーター

s
ReadOnlySpan<Char>

変換する数値を含む読み取り専用の文字スパン。 スパンは、styleで指定されたスタイルを使用して解釈されます。

style
NumberStyles

sの許可された形式を示す列挙値のビットごとの組み合わせ。 指定する一般的な値は、AllowThousandsと組み合わせて Float

provider
IFormatProvider

sに関するカルチャ固有の書式設定情報を提供するオブジェクト。

result
Single

このメソッドが戻るときに、変換に成功した場合は、sに含まれる数値または記号に相当する単精度浮動小数点数を格納し、変換に失敗した場合は 0 を格納します。 パラメーターが または に準拠している形式ではない場合、または Single.MaxValueより小さい数値、または Single.MaxValueより大きい 数を表す場合、または が列挙定数 有効な組み合わせでない場合、変換は失敗します。 このパラメーターは初期化されていない状態で渡されます。result で最初に指定された値は上書きされます。

戻り値

s が正常に変換されたかどうかを true します。それ以外の場合は、falseします。

注釈

.NET Core 3.0 以降では、IEEE 754 仕様で必要に応じて、大きすぎる値は PositiveInfinity または NegativeInfinity に丸められます。 以前のバージョン (.NET Framework を含む) では、大きすぎる値を解析するとエラーが発生しました。

適用対象

TryParse(String, NumberStyles, IFormatProvider, Single)

ソース:
Single.cs
ソース:
Single.cs
ソース:
Single.cs

指定したスタイルおよびカルチャ固有の形式の数値の文字列形式を、等価の単精度浮動小数点数に変換します。 戻り値は、変換が成功したか失敗したかを示します。

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

パラメーター

s
String

変換する数値を表す文字列。

style
NumberStyles

sの許可された形式を示す列挙値のビットごとの組み合わせ。 指定する一般的な値は、AllowThousandsと組み合わせて Float

provider
IFormatProvider

sに関するカルチャ固有の書式設定情報を提供するオブジェクト。

result
Single

このメソッドが戻るときに、変換に成功した場合は、sに含まれる数値または記号に相当する単精度浮動小数点数を格納し、変換に失敗した場合は 0 を格納します。 s パラメーターが null または Emptystyleに準拠している形式でない場合、または styleNumberStyles 列挙定数の有効な組み合わせでない場合、変換は失敗します。 また、.NET Framework または .NET Core 2.2 以前のバージョンでも、 が Single.MinValue より小さいか、Single.MaxValueより大きい数値を表している場合は失敗します。 このパラメーターは初期化されていない状態で渡されます。result で最初に指定された値は上書きされます。

戻り値

s が正常に変換されたかどうかを true します。それ以外の場合は、falseします。

例外

styleNumberStyles 値ではありません。

-又は-

styleAllowHexSpecifier 値です。

次の例では、Single.TryParse(String, NumberStyles, IFormatProvider, Single) メソッドを使用して、特定のスタイルを持ち、特定のカルチャの規則を使用して書式設定された数値の文字列形式を解析する方法を示します。

string value;
System.Globalization.NumberStyles style;
System.Globalization.CultureInfo culture;
float number;

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

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

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

value = "1 345,978";
if (Single.TryParse(value, style, culture, out number))
   Console.WriteLine("Converted '{0}' to {1}.", value, number);
else
   Console.WriteLine("Unable to convert '{0}'.", value);
// The example displays the following output:
//       Converted '£1,097.63' to 1097.63.
//       Converted '1345,978' to 1345.978.
//       Converted '1.345,978' to 1345.978.
//       Unable to convert '1 345,978'.
// Parse currency value using en-GB culture.
let value = "£1,097.63"
let style = System.Globalization.NumberStyles.Number ||| System.Globalization.NumberStyles.AllowCurrencySymbol
let culture = System.Globalization.CultureInfo.CreateSpecificCulture "en-GB"
match Single.TryParse(value, style, culture) with
| true, number ->
    printfn $"Converted '{value}' to {number}."
| _ ->
    printfn $"Unable to convert '{value}'."

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

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

let value = "1 345,978"
match Single.TryParse(value, style, culture) with
| true, number ->
    printfn $"Converted '{value}' to {number}."
| _ ->
    printfn $"Unable to convert '{value}'."
// The example displays the following output:
//       Converted '£1,097.63' to 1097.63.
//       Converted '1345,978' to 1345.978.
//       Converted '1.345,978' to 1345.978.
//       Unable to convert '1 345,978'.
Dim value As String
Dim style As System.Globalization.NumberStyles
Dim culture As System.Globalization.CultureInfo
Dim number As Single

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

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

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

value = "1 345,978"
If Single.TryParse(value, style, culture, number) Then
   Console.WriteLine("Converted '{0}' to {1}.", value, number)
Else
   Console.WriteLine("Unable to convert '{0}'.", value)
End If
' The example displays the following output:
'       Converted '£1,097.63' to 1097.63.
'       Converted '1345,978' to 1345.978.
'       Converted '1.345,978' to 1345.978.
'       Unable to convert '1 345,978'.

注釈

.NET Core 3.0 以降では、IEEE 754 仕様で必要に応じて、大きすぎる値は PositiveInfinity または NegativeInfinity に丸められます。 以前のバージョン (.NET Framework を含む) では、大きすぎる値を解析するとエラーが発生しました。

このオーバーロードは、解析された数値を返す代わりに解析操作が成功したかどうかを示すブール値を返すことによって、Parse(String, NumberStyles, IFormatProvider) メソッドとは異なります。 これにより、s が無効であり、正常に解析できない場合に、例外処理を使用して FormatException をテストする必要がなくなります。

style パラメーターは、解析操作が成功するための s パラメーターの許容形式を定義します。 NumberStyles 列挙型のビット フラグの組み合わせである必要があります。 次の NumberStyles メンバーはサポートされていません。

s パラメーターには、providerで示されるカルチャの PositiveInfinitySymbolNegativeInfinitySymbolNaNSymbol を含めることができます。 さらに、styleの値によっては、s パラメーターに次の要素が含まれる場合があります。

[ws][$][sign][整数桁,]整数桁[.fractional-digits][e[sign]exponential-digits][ws]

角かっこ ([ と ]) の要素は省略可能です。 次の表では、各要素について説明します。

要素 形容
ws の 省略可能な空白。 NumberStyles.AllowLeadingWhite フラグが含まれている場合は、s の先頭に空白 style 表示できます。 styleNumberStyles.AllowTrailingWhite フラグが含まれている場合は、s の末尾に表示されます。
$ カルチャ固有の通貨記号。 文字列内での位置は、provider パラメーターの IFormatProvider.GetFormat メソッドによって返される NumberFormatInfo オブジェクトの NumberFormatInfo.CurrencyNegativePattern または NumberFormatInfo.CurrencyPositivePattern プロパティによって定義されます。 styleNumberStyles.AllowCurrencySymbol フラグが含まれている場合、通貨記号は s に表示されます。
sign 省略可能な記号。 styleNumberStyles.AllowLeadingSign フラグが含まれている場合は s の先頭に表示され、NumberStyles.AllowTrailingSign フラグ style 含まれている場合は s の末尾に表示されます。 s でかっこを使用すると、styleNumberStyles.AllowParentheses フラグが含まれている場合に負の値を示すことができます。
整数桁の 数値の整数部分を指定する 0 から 9 までの一連の数字。 整数桁は、小数部が存在する場合は使用できません。
カルチャ固有の桁区切り記号。 現在のカルチャの桁区切り記号は、NumberStyles.AllowThousands フラグ style 含まれている場合、s に表示されます。
. カルチャ固有の小数点記号。 styleNumberStyles.AllowDecimalPoint フラグが含まれている場合、現在のカルチャの小数点記号は s に表示されます。
小数部の を する 数値の小数部を指定する 0 から 9 までの一連の数字。 styleNumberStyles.AllowDecimalPoint フラグが含まれている場合、小数部の数字は s に表示されます。
e e または E 文字。指数表記を使用して s が数値を表すことができることを示します。 スタイルに NumberStyles.AllowExponent フラグが含まれている場合、s パラメーターは指数表記で数値を表すことができます。
指数桁 を する 指数を指定する 0 から 9 までの一連の数字。

手記

s で終了する NUL (U+0000) 文字は、style 引数の値に関係なく、解析操作では無視されます。

数字のみを含む文字列 (NumberStyles.None スタイルに対応) は、Single 型の範囲内にある場合、常に正常に解析されます。 残りの System.Globalization.NumberStyles メンバーは、入力文字列に存在する必要がない要素を制御します。 次の表は、個々の NumberStyles フラグが、sに存在する可能性がある要素に与える影響を示しています。

NumberStyles 値 数字に加えて s で許可される要素
None 整数桁 要素のみ。
AllowDecimalPoint .要素の小数部 および します。
AllowExponent s パラメーターでは指数表記を使用することもできます。 このフラグは、E指数桁整数桁 形式の値をサポートしています。正符号や負符号、小数点記号などの要素を使用して指数表記で文字列を正常に解析するには、追加のフラグが必要です。
AllowLeadingWhite sの先頭にある ws 要素。
AllowTrailingWhite sの末尾にある ws 要素。
AllowLeadingSign sの先頭にある 記号 要素です。
AllowTrailingSign の末尾にある要素 記号。
AllowParentheses 符号、数値を囲むかっこの形式で要素に署名します。
AllowThousands 要素。
AllowCurrencySymbol $ 要素。
Currency すべての。 s パラメーターは、指数表記で 16 進数または数値を表すことはできません。
Float sの先頭または末尾にある ws 要素、sの先頭 符号、および 記号。 s パラメーターでは指数表記を使用することもできます。
Number wssign、桁区切り記号 (,)、 および小数点 (.) 要素。
Any s を除くすべてのスタイルは、16 進数を表すことはできません。

provider パラメーターは、GetFormat メソッドがカルチャ固有の書式設定情報を提供する NumberFormatInfo オブジェクトを返す IFormatProvider 実装です。 TryParse(String, NumberStyles, IFormatProvider, Single) メソッドが呼び出されると、provider パラメーターの GetFormat メソッドが呼び出され、NumberFormatInfo 型を表す Type オブジェクトが渡されます。 GetFormat メソッドは、s パラメーターの形式に関する情報を提供する NumberFormatInfo オブジェクトを返します。 provider パラメーターを使用して、解析操作にカスタム書式情報を指定するには、次の 3 つの方法があります。

  • 書式設定情報を提供するカルチャを表す CultureInfo オブジェクトを渡すことができます。 その GetFormat メソッドは、そのカルチャの数値書式情報を提供する NumberFormatInfo オブジェクトを返します。

  • 数値書式情報を提供する実際の NumberFormatInfo オブジェクトを渡すことができます。 (GetFormat の実装はそれ自体を返すだけです)。

  • IFormatProviderを実装するカスタム オブジェクトを渡すことができます。 その GetFormat メソッドは、書式設定情報を提供する NumberFormatInfo オブジェクトをインスタンス化して返します。

providernull場合、s の書式設定は、現在のカルチャの NumberFormatInfo オブジェクトに基づいて解釈されます。

sSingle データ型の範囲外の場合、メソッドは .NET Framework および .NET Core 2.2 以前のバージョンで OverflowException をスローします。 .NET Core 3.0 以降のバージョンでは、sSingle.MinValue 未満の場合は Single.NegativeInfinity を返し、sSingle.MaxValueより大きい場合は Single.PositiveInfinity

解析操作中に s パラメーターで区切り記号が検出され、該当する通貨または数値の小数点とグループの区切り記号が同じである場合、解析操作では、区切り記号がグループ区切り記号ではなく小数点であると見なされます。 区切り記号の詳細については、「CurrencyDecimalSeparatorNumberDecimalSeparatorCurrencyGroupSeparator、および NumberGroupSeparator」を参照してください。

こちらもご覧ください

適用対象