次の方法で共有


Decimal.TryParse メソッド

定義

数値の文字列形式を等価の Decimal に変換します。 戻り値は、変換が成功したか失敗したかを示します。

オーバーロード

TryParse(ReadOnlySpan<Byte>, Decimal)

数値の文字列形式を含む UTF-8 文字スパンを、符号付き 10 進数に変換しようとします。

TryParse(ReadOnlySpan<Char>, Decimal)

指定したスタイルとカルチャ固有の形式を使用して、数値のスパン表現を同等の Decimal に変換します。 戻り値は、変換が成功したか失敗したかを示します。

TryParse(String, Decimal)

数値の文字列形式を等価の Decimal に変換します。 戻り値は、変換が成功したか失敗したかを示します。

TryParse(ReadOnlySpan<Byte>, IFormatProvider, Decimal)

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

TryParse(ReadOnlySpan<Char>, IFormatProvider, Decimal)

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

TryParse(String, IFormatProvider, Decimal)

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

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

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

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

指定したスタイルとカルチャ固有の形式を使用して、数値のスパン表現を同等の Decimal に変換します。 戻り値は、変換が成功したか失敗したかを示します。

TryParse(String, NumberStyles, IFormatProvider, Decimal)

指定したスタイルとカルチャ固有の形式を使用して、数値の文字列形式を等価の Decimal に変換します。 戻り値は、変換が成功したか失敗したかを示します。

TryParse(ReadOnlySpan<Byte>, Decimal)

ソース:
Decimal.cs
ソース:
Decimal.cs

数値の文字列形式を含む UTF-8 文字スパンを、符号付き 10 進数に変換しようとします。

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

パラメーター

utf8Text
ReadOnlySpan<Byte>

変換する数値を表す UTF-8 文字を含むスパン。

result
Decimal

このメソッドから制御が戻るときに、変換に成功した場合は utf8Text に含まれる数値に相当する符号付き 10 進値を格納し、変換に失敗した場合は 0 を格納します。 このパラメーターは初期化されていない状態で渡されます。結果で最初に指定された値は上書きされます。

戻り値

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

適用対象

TryParse(ReadOnlySpan<Char>, Decimal)

ソース:
Decimal.cs
ソース:
Decimal.cs
ソース:
Decimal.cs

指定したスタイルとカルチャ固有の形式を使用して、数値のスパン表現を同等の Decimal に変換します。 戻り値は、変換が成功したか失敗したかを示します。

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

パラメーター

s
ReadOnlySpan<Char>

変換する数値を表す文字を含むスパン。

result
Decimal

このメソッドから制御が戻るときに、変換に成功した場合は sに含まれる数値に相当する Decimal 番号を格納し、変換に失敗した場合は 0 を格納します。 パラメーターが または の場合、または に準拠した形式の数値ではない場合、または Decimal.MinValue より小さい数値、または Decimal.MaxValueより大きい数値 表す場合、変換は失敗します。 このパラメーターは uininitialized に渡されます。result で最初に指定された値が上書きされます。

戻り値

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

適用対象

TryParse(String, Decimal)

ソース:
Decimal.cs
ソース:
Decimal.cs
ソース:
Decimal.cs

数値の文字列形式を等価の Decimal に変換します。 戻り値は、変換が成功したか失敗したかを示します。

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

パラメーター

s
String

変換する数値の文字列形式。

result
Decimal

このメソッドから制御が戻るときに、変換に成功した場合は sに含まれる数値に相当する Decimal 番号を格納し、変換に失敗した場合は 0 を格納します。 パラメーターが または の場合、有効な形式の数値ではない場合、または Decimal.MinValue より小さい数値、または Decimal.MaxValueより大きい数値 表す場合、変換は失敗します。 このパラメーターは uininitialized に渡されます。result で最初に指定された値が上書きされます。

戻り値

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

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

string value;
decimal number;

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

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

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

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

// Parse a negative integer value.
let value = "-1689346178821"
match Decimal.TryParse value with
| true, number ->
    printfn $"{number}"
| _ -> 
    printfn $"Unable to parse '{value}'."
// The example displays the following output to the console:
//       1643.57
//       Unable to parse '$1,643.57'.
//       Unable to parse '-1.643e6'.
//       -1689346178821
Dim value As String
Dim number As Decimal

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

' Parse a negative integer value.
value = "-1689346178821"
If Decimal.TryParse(value, number)
   Console.WriteLine(number)
Else
   Console.WriteLine("Unable to parse '{0}'.", value)   
End If
' The example displays the following output to the console:
'       1643.57
'       Unable to parse '$1,643.57'.
'       Unable to parse '-1.643e6'.
'       -1689346178821

注釈

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

パラメーター s には、次の形式が含まれています。

[ws][sign][digits,]digits[.fractional-digits][ws]

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

要素 形容
ws の 省略可能な空白。
sign 省略可能な記号。
桁の 0 から 9 までの数字のシーケンス。
カルチャ固有の桁区切り記号。
. カルチャ固有の小数点記号。
小数部の を する 0 から 9 までの数字のシーケンス。

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

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

必要に応じて、s の値は、最も近い四捨五入を使用して丸められます。

Decimal オブジェクトの有効桁数は 29 桁です。 s が 29 桁を超え、小数部を持ち、MaxValue および MinValueの範囲内にある数値を表す場合、最も近い四捨五入を使用して、数値は切り捨てられず、29 桁に丸められます。

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

こちらもご覧ください

適用対象

TryParse(ReadOnlySpan<Byte>, IFormatProvider, Decimal)

ソース:
Decimal.cs
ソース:
Decimal.cs

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

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

パラメーター

utf8Text
ReadOnlySpan<Byte>

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

provider
IFormatProvider

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

result
Decimal

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

戻り値

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

適用対象

TryParse(ReadOnlySpan<Char>, IFormatProvider, Decimal)

ソース:
Decimal.cs
ソース:
Decimal.cs
ソース:
Decimal.cs

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

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

パラメーター

s
ReadOnlySpan<Char>

解析する文字のスパン。

provider
IFormatProvider

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

result
Decimal

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

戻り値

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

適用対象

TryParse(String, IFormatProvider, Decimal)

ソース:
Decimal.cs
ソース:
Decimal.cs
ソース:
Decimal.cs

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

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

パラメーター

s
String

解析する文字列。

provider
IFormatProvider

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

result
Decimal

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

戻り値

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

適用対象

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

ソース:
Decimal.cs
ソース:
Decimal.cs

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

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

パラメーター

utf8Text
ReadOnlySpan<Byte>

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

style
NumberStyles

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

provider
IFormatProvider

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

result
Decimal

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

戻り値

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

適用対象

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

ソース:
Decimal.cs
ソース:
Decimal.cs
ソース:
Decimal.cs

指定したスタイルとカルチャ固有の形式を使用して、数値のスパン表現を同等の Decimal に変換します。 戻り値は、変換が成功したか失敗したかを示します。

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

パラメーター

s
ReadOnlySpan<Char>

変換する数値を表す文字を含むスパン。

style
NumberStyles

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

provider
IFormatProvider

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

result
Decimal

このメソッドから制御が戻るときに、変換に成功した場合は sに含まれる数値に相当する Decimal 番号を格納し、変換に失敗した場合は 0 を格納します。 パラメーターが または の場合、または に準拠した形式の数値ではない場合、または Decimal.MinValue より小さい数値、または Decimal.MaxValueより大きい数値 表す場合、変換は失敗します。 このパラメーターは uininitialized に渡されます。result で最初に指定された値が上書きされます。

戻り値

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

適用対象

TryParse(String, NumberStyles, IFormatProvider, Decimal)

ソース:
Decimal.cs
ソース:
Decimal.cs
ソース:
Decimal.cs

指定したスタイルとカルチャ固有の形式を使用して、数値の文字列形式を等価の Decimal に変換します。 戻り値は、変換が成功したか失敗したかを示します。

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

パラメーター

s
String

変換する数値の文字列形式。

style
NumberStyles

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

provider
IFormatProvider

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

result
Decimal

このメソッドから制御が戻るときに、変換に成功した場合は sに含まれる数値に相当する Decimal 番号を格納し、変換に失敗した場合は 0 を格納します。 パラメーターが または の場合、または に準拠した形式の数値ではない場合、または Decimal.MinValue より小さい数値、または Decimal.MaxValueより大きい数値 表す場合、変換は失敗します。 このパラメーターは uininitialized に渡されます。result で最初に指定された値が上書きされます。

戻り値

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

例外

styleNumberStyles 値ではありません。

-又は-

styleAllowHexSpecifier 値です。

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

string value;
NumberStyles style;
CultureInfo culture;
decimal number;

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

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

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

value = "1 345,978";
if (Decimal.TryParse(value, style, culture, out number))
   Console.WriteLine("Converted '{0}' to {1}.", value, number);
else
   Console.WriteLine("Unable to convert '{0}'.", value);
// Displays:
//       Unable to convert '1 345,978'.
// Parse currency value using en-GB culture.
let value = "£1,097.63"
let style = NumberStyles.Number ||| NumberStyles.AllowCurrencySymbol
let culture = CultureInfo.CreateSpecificCulture "en-GB"
match Decimal.TryParse(value, style, culture) with
| true, number ->
    printfn $"Converted '{value}' to {number}."
| _ -> 
    printfn $"Unable to convert '{value}'."
// Displays:
//       Converted '£1,097.63' to 1097.63.

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

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

let value = "1 345,978"
match Decimal.TryParse(value, style, culture) with
| true, number ->
    printfn $"Converted '{value}' to {number}."
| _ -> 
    printfn $"Unable to convert '{value}'."
// Displays:
//       Unable to convert '1 345,978'.
Dim value As String
Dim style As NumberStyles
Dim culture As CultureInfo
Dim number As Decimal

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

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

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

value = "1 345,978"
If Decimal.TryParse(value, style, culture, number) Then
   Console.WriteLine("Converted '{0}' to {1}.", value, number)
Else
   Console.WriteLine("Unable to convert '{0}'.", value)
End If    
' Displays:
'       Unable to convert '1 345,978'.

注釈

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

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

スタイルの値によっては、s パラメーターに次の要素が含まれる場合があります。

[ws][$][sign][digits,]digits[.fractional-digits][e[sign]digits][ws]

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

要素 形容
ws の 省略可能な空白。 NumberStyles.AllowLeadingWhite フラグが含まれている場合は、s の先頭に空白 style 表示できます。 styleNumberStyles.AllowTrailingWhite フラグが含まれている場合は、s の末尾に表示されます。
$ カルチャ固有の通貨記号。 文字列内での位置は、provider パラメーターの IFormatProvider.GetFormat メソッドによって返される NumberFormatInfo オブジェクトの NumberFormatInfo.CurrencyNegativePattern または NumberFormatInfo.CurrencyPositivePattern プロパティによって定義されます。 styleNumberStyles.AllowCurrencySymbol フラグが含まれている場合、通貨記号は s に表示されます。
sign 省略可能な記号。
桁の 0 から 9 までの数字のシーケンス。
. カルチャ固有の小数点記号。
小数部の を する 0 から 9 までの数字のシーケンス。

style パラメーターは、s パラメーターの許可される形式を指定し、ビットごとの OR 演算を使用して組み合わせた 1 つ以上の NumberStyles 列挙定数を指定できます。 style が null の場合、sNumberStyles.Number スタイルを使用して解釈されます。

provider パラメーターは、NumberFormatInfoCultureInfo オブジェクトなどの IFormatProvider 実装です。 provider パラメーターは、解析に使用されるカルチャ固有の情報を提供します。 providernullされている場合は、スレッドの現在のカルチャが使用されます。

Decimal オブジェクトの有効桁数は 29 桁です。 s が 29 桁を超え、小数部を持ち、MaxValue および MinValueの範囲内にある数値を表す場合、最も近い四捨五入を使用して、数値は切り捨てられず、29 桁に丸められます。

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

こちらもご覧ください

適用対象