Decimal.TryParse メソッド
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
数値の文字列形式を、それと等価の 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 を格納します。 このパラメーターは、初期化されていない状態で渡されます。result にもともと入っていた値は上書きされます。
戻り値
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 を格納します。 パラメーターが null
または Emptyの場合、または に準拠style
している形式の数値ではない場合、または Decimal.MinValue より小さい数値または Decimal.MaxValue より大きい数値を表す場合s
、変換は失敗します。 このパラメーターは初期化されずに渡されます。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 を格納します。 パラメーターが または Emptyの場合、null
有効な形式の数値ではない場合、または Decimal.MinValue より小さい数値または Decimal.MaxValue より大きい数値を表す場合s
、変換は失敗します。 このパラメーターは初期化されずに渡されます。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 桁です。 が 29 桁を超えるが、小数部を持ち、 と のMaxValueMinValue範囲内にある数値を表す場合s
、最も近い四捨五入を使用して、数値は切り捨てられず、29 桁に丸められます。
解析操作中にパラメーターで s
区切り記号が検出され、該当する通貨または数値の小数点とグループ区切り記号が同じである場合、解析操作では、区切り記号がグループ区切り記号ではなく小数点の区切り記号であると見なされます。 区切り記号の詳細については、「、、、および 」を参照してくださいCurrencyDecimalSeparatorCurrencyGroupSeparatorNumberDecimalSeparator。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
された結果、または失敗した場合に未定義の値が含まれます。
戻り値
true
が正常に解析された場合 utf8Text
は 。それ以外の場合 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
された結果、または失敗した場合に未定義の値が格納されます。
戻り値
true
が正常に解析された場合 s
は 。それ以外の場合 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
された結果または失敗した場合に未定義の値が含まれます。
戻り値
true
が正常に解析された場合 s
は 。それ以外の場合 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
された結果または失敗した場合の未定義の値が含まれます。
戻り値
true
正常に解析された場合 utf8Text
は 。それ以外の場合 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 を格納します。 パラメーターが または である場合、または Emptyが null
に準拠style
している形式の数値ではない場合、または Decimal.MinValue より小さい数値または Decimal.MaxValue より大きい数値を表す場合s
、変換は失敗します。 このパラメーターは初期化されずに渡されます。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 を格納します。 パラメーターが または である場合、または Emptyが null
に準拠style
している形式の数値ではない場合、または Decimal.MinValue より小さい数値または Decimal.MaxValue より大きい数値を表す場合s
、変換は失敗します。 このパラメーターは初期化されずに渡されます。result
で最初に指定された値はすべて上書きされます。
戻り値
s
が正常に変換された場合は true
。それ以外の場合は false
。
例外
例
次の例では、 メソッドを 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 のメンバーはサポートされていません。
style の値に応じて、 パラメーターに s
次の要素が含まれる場合があります。
[ws][$][sign][digits,]digits[.fractional-digits][e[sign]digits][ws]
角かっこ ([ および ]) 内の要素は省略可能です。 次の表は、それぞれの要素の説明です。
要素 | 説明 |
---|---|
ws | オプションの空白。 フラグが含まれている場合style は、 のs 先頭に空白をNumberStyles.AllowLeadingWhite表示できます。 フラグが含まれている場合style は、 のs 末尾にNumberStyles.AllowTrailingWhite表示されます。 |
$ | カルチャ固有の通貨記号。 文字列内の位置は、 パラメーターの NumberFormatInfo.CurrencyNegativePattern メソッドprovider によって返される オブジェクトの NumberFormatInfo または NumberFormatInfo.CurrencyPositivePattern プロパティによってIFormatProvider.GetFormat定義されます。 に フラグが含まれている場合style は、通貨記号を にs NumberStyles.AllowCurrencySymbol表示できます。 |
sign | 省略可能な記号。 |
数値 | 0 から 9 までの数字のシーケンス。 |
. | カルチャ固有の小数点記号。 |
小数部の桁数 | 0 から 9 までの数字のシーケンス。 |
パラメーターは style
、パラメーターの許可される形式を s
指定し、ビットごとの OR 演算を使用して組み合わせた 1 つ以上 NumberStyles の列挙定数を指定できます。 が null の場合 style
、 s
は スタイルを NumberStyles.Number 使用して解釈されます。
パラメーターはprovider
、 IFormatProvider や CultureInfo オブジェクトなどのNumberFormatInfo実装です。 パラメーターは provider
、解析で使用されるカルチャ固有の情報を提供します。 provider
が null
の場合は、スレッドの現在のカルチャが使用されます。
オブジェクトの Decimal 有効桁数は 29 桁です。 が 29 桁を超え、小数部を持ち、 と MinValueのMaxValue範囲内にある数値を表す場合s
、最も近い四捨五入を使用して、数値は切り捨てられず、29 桁に丸められます。
解析操作中にパラメーターで s
区切り記号が検出され、該当する通貨または数値の小数点とグループ区切り記号が同じである場合、解析操作では、区切り記号がグループ区切り記号ではなく小数点の区切り記号であると見なされます。 区切り記号の詳細については、「、、、および 」を参照してくださいCurrencyDecimalSeparatorCurrencyGroupSeparatorNumberDecimalSeparator。NumberGroupSeparator
こちらもご覧ください
適用対象
.NET
フィードバック
https://aka.ms/ContentUserFeedback」を参照してください。
以下は間もなく提供いたします。2024 年を通じて、コンテンツのフィードバック メカニズムとして GitHub の issue を段階的に廃止し、新しいフィードバック システムに置き換えます。 詳細については、「フィードバックの送信と表示