次の方法で共有


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
ソース:
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 解析された結果または失敗した場合の未定義の値が格納されます。

返品

true sが正常に解析された場合は。それ以外の場合はfalse

適用対象

TryParse(ReadOnlySpan<Char>, Single)

ソース:
Single.cs
ソース:
Single.cs
ソース:
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または空であるか、有効な形式の数値でない場合、変換は失敗します。 sSingle.MinValue より小さい有効な数値である場合、resultNegativeInfinitysSingle.MaxValue より大きい有効な数の場合、resultPositiveInfinity。 このパラメーターは初期化されていない状態で渡されます。 result で最初に指定された値は上書きされます。

返品

が正常に変換された場合は a0/&。それ以外の場合は。

注釈

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

適用対象

TryParse(String, Single)

ソース:
Single.cs
ソース:
Single.cs
ソース:
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であるか、有効な形式の数値でない場合、変換は失敗します。 また、 sSingle.MinValue より小さいか、Single.MaxValue より大きい数値を表す場合、.NET Framework でも失敗します。 このパラメーターは初期化されていない状態で渡されます。 result で最初に指定された値は上書きされます。

返品

が正常に変換された場合は a0/&。それ以外の場合は。

次の例では、 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) メソッドとは異なります。 これにより、FormatExceptionが無効であり、正常に解析できない場合に、例外処理を使用してsをテストする必要がなくなります。

s パラメーターには、PositiveInfinitySymbolNegativeInfinitySymbol、またはNaNSymbolシンボルを含めることができます。 この文字列比較は、.NET Core 3.0 以降のバージョンでは大文字と小文字は区別されませんが、.NET Framework を含む以前のバージョンでは大文字と小文字が区別されます。 s パラメーターには、次の形式の文字列を指定することもできます。

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

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

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

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

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

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

sSingleデータ型の範囲外の場合、メソッドは .NET Framework 上のfalseを返します。 .NET Core 3.0 以降のバージョンでは、Single.NegativeInfinitys未満の場合はSingle.MinValueを返し、Single.PositiveInfinitysより大きい場合はSingle.MaxValueします。

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

こちらもご覧ください

適用対象

TryParse(ReadOnlySpan<Byte>, IFormatProvider, Single)

ソース:
Single.cs
ソース:
Single.cs
ソース:
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 が正常に解析された結果、または失敗した場合に未定義の値が含まれます。

返品

true utf8Textが正常に解析された場合は。それ以外の場合はfalse

適用対象

TryParse(ReadOnlySpan<Char>, IFormatProvider, Single)

ソース:
Single.cs
ソース:
Single.cs
ソース:
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の解析に成功した結果、または失敗した場合は未定義の値が格納されます。

返品

true sが正常に解析された場合は。それ以外の場合はfalse

適用対象

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

ソース:
Single.cs
ソース:
Single.cs
ソース:
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 が正常に解析された結果、または失敗した場合に未定義の値が含まれます。

返品

true utf8Textが正常に解析された場合は。それ以外の場合はfalse

適用対象

TryParse(ReadOnlySpan<Byte>, Single)

ソース:
Single.cs
ソース:
Single.cs
ソース:
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されているか、有効な形式でない場合、変換は失敗します。 このパラメーターは初期化されていない状態で渡されます。結果で最初に指定された値は上書きされます。

返品

が正常に変換された場合は a0/&。それ以外の場合は。

適用対象

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

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

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

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, [Runtime::InteropServices::Out] float % result);
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の許可される形式を示す列挙値のビットごとの組み合わせ。 指定する一般的な値は、Floatと組み合わせてAllowThousands

provider
IFormatProvider

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

result
Single

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

返品

が正常に変換された場合は a0/&。それ以外の場合は。

注釈

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

適用対象

TryParse(String, NumberStyles, IFormatProvider, Single)

ソース:
Single.cs
ソース:
Single.cs
ソース:
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の許可される形式を示す列挙値のビットごとの組み合わせ。 指定する一般的な値は、Floatと組み合わせてAllowThousands

provider
IFormatProvider

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

result
Single

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

返品

が正常に変換された場合は a0/&。それ以外の場合は。

例外

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) メソッドとは異なります。 これにより、FormatExceptionが無効であり、正常に解析できない場合に、例外処理を使用してsをテストする必要がなくなります。

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

s パラメーターには、providerで示されるカルチャのPositiveInfinitySymbolNegativeInfinitySymbol、またはNaNSymbolシンボルを含めることができます。 この文字列比較は、.NET Core 3.0 以降のバージョンでは大文字と小文字は区別されませんが、.NET Framework を含む以前のバージョンでは大文字と小文字が区別されます。 さらに、 styleの値によっては、 s パラメーターに次の要素が含まれる場合があります。

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

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

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

手記

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

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

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

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

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

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

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

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

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

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

こちらもご覧ください

適用対象