Decimal.Parse メソッド

定義

数値の文字列形式を、それと等価の Decimal に変換します。

オーバーロード

Parse(String)

数値の文字列形式を、それと等価の Decimal に変換します。

Parse(ReadOnlySpan<Byte>, IFormatProvider)

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

Parse(ReadOnlySpan<Char>, IFormatProvider)

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

Parse(String, NumberStyles)

指定のスタイルで表現された数値の文字列形式を、それと等価な Decimal に変換します。

Parse(String, IFormatProvider)

指定したカルチャ固有の書式情報を使用して、文字列形式の数値をそれと等価の Decimal に変換します。

Parse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider)

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

Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider)

指定したスタイルとカルチャ固有の書式を使用して、数値のスパン表現を等価の Decimal に変換します。

Parse(String, NumberStyles, IFormatProvider)

指定したスタイルとカルチャ固有の書式を使用して、文字列形式の数値をそれと等価の Decimal に変換します。

Parse(String)

数値の文字列形式を、それと等価の Decimal に変換します。

public:
 static System::Decimal Parse(System::String ^ s);
public static decimal Parse (string s);
static member Parse : string -> decimal
Public Shared Function Parse (s As String) As Decimal

パラメーター

s
String

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

戻り値

s に含まれている数値と等価。

例外

snullです。

s が正しい形式ではありません。

s は、 Decimal.MinValue より小さい数値または Decimal.MaxValue より大きい数値を表します。

次のコード例では、 メソッドを Parse(String) 使用して値の Decimal 文字列表現を解析します。

string value;
decimal number;
// Parse an integer with thousands separators.
value = "16,523,421";
number = Decimal.Parse(value);
Console.WriteLine("'{0}' converted to {1}.", value, number);
// Displays:
//    '16,523,421' converted to 16523421.

// Parse a floating point value with thousands separators
value = "25,162.1378";
number = Decimal.Parse(value);
Console.WriteLine("'{0}' converted to {1}.", value, number);
// Displays:
//    '25,162.1378' converted to 25162.1378.

// Parse a floating point number with US currency symbol.
value = "$16,321,421.75";
try
{
   number = Decimal.Parse(value);
   Console.WriteLine("'{0}' converted to {1}.", value, number);
}
catch (FormatException)
{
   Console.WriteLine("Unable to parse '{0}'.", value);
}
// Displays:
//    Unable to parse '$16,321,421.75'.

// Parse a number in exponential notation
value = "1.62345e-02";
try
{
   number = Decimal.Parse(value);
   Console.WriteLine("'{0}' converted to {1}.", value, number);
}
catch (FormatException)
{
   Console.WriteLine("Unable to parse '{0}'.", value);
}
// Displays:
//    Unable to parse '1.62345e-02'.
// Parse an integer with thousands separators.
let value = "16,523,421"
let number = Decimal.Parse value
printfn $"'{value}' converted to {number}."
// Displays:
//    '16,523,421' converted to 16523421.

// Parse a floating point value with thousands separators
let value = "25,162.1378"
let number = Decimal.Parse value
printfn $"'{value}' converted to {number}."
// Displays:
//    '25,162.1378' converted to 25162.1378.

// Parse a floating point number with US currency symbol.
let value = "$16,321,421.75"
try
    let number = Decimal.Parse value
    printfn $"'{value}' converted to {number}."
with :? FormatException ->
    printfn $"Unable to parse '{value}'."
// Displays:
//    Unable to parse '$16,321,421.75'.

// Parse a number in exponential notation
let value = "1.62345e-02"
try
    let number = Decimal.Parse value
    printfn $"'{value}' converted to {number}."
with :? FormatException ->
    printfn $"Unable to parse '{value}'."
// Displays:
//    Unable to parse '1.62345e-02'.
Dim value As String
Dim number As Decimal

' Parse an integer with thousands separators. 
value = "16,523,421"
number = Decimal.Parse(value)
Console.WriteLine("'{0}' converted to {1}.", value, number)
' Displays: 
'    '16,523,421' converted to 16523421.

' Parse a floating point value with thousands separators
value = "25,162.1378"
number = Decimal.Parse(value)
Console.WriteLine("'{0}' converted to {1}.", value, number)
' Displays:
'    '25,162.1378' converted to 25162.1378.

' Parse a floating point number with US currency symbol.
value = "$16,321,421.75"
Try
   number = Decimal.Parse(value)
   Console.WriteLine("'{0}' converted to {1}.", value, number)
Catch e As FormatException
   Console.WriteLine("Unable to parse '{0}'.", value)
End Try
' Displays:
'    Unable to parse '$16,321,421.75'.  

' Parse a number in exponential notation
value = "1.62345e-02"
Try
   number = Decimal.Parse(value)
   Console.WriteLine("'{0}' converted to {1}.", value, number)
Catch e As FormatException
   Console.WriteLine("Unable to parse '{0}'.", value)
End Try
' Displays: 
'    Unable to parse '1.62345e-02'.

注釈

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

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

角かっこ ([ および ]) 内の要素は省略可能です。 次の表は、それぞれの要素の説明です。

要素 説明
ws オプションの空白。
sign 省略可能な記号。
数値 0 から 9 までの数字のシーケンス。
, カルチャ固有の桁区切り記号。
. カルチャ固有の小数点記号。
小数部の桁数 0 から 9 までの数字のシーケンス。

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

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

必要に応じて、 の s 値を最も近い値に丸めて丸めます。

には Decimal 29 桁の有効桁数があります。 が 29 桁を超え、小数部を持ち、 と MinValueMaxValue範囲内にある数値を表す場合s、最も近い四捨五入を使用して、数値は切り捨てられず、29 桁に丸められます。

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

こちらもご覧ください

適用対象

Parse(ReadOnlySpan<Byte>, IFormatProvider)

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

public:
 static System::Decimal Parse(ReadOnlySpan<System::Byte> utf8Text, IFormatProvider ^ provider) = IUtf8SpanParsable<System::Decimal>::Parse;
public static decimal Parse (ReadOnlySpan<byte> utf8Text, IFormatProvider? provider);
static member Parse : ReadOnlySpan<byte> * IFormatProvider -> decimal
Public Shared Function Parse (utf8Text As ReadOnlySpan(Of Byte), provider As IFormatProvider) As Decimal

パラメーター

utf8Text
ReadOnlySpan<Byte>

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

provider
IFormatProvider

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

戻り値

を解析した utf8Text結果。

実装

適用対象

Parse(ReadOnlySpan<Char>, IFormatProvider)

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

public:
 static System::Decimal Parse(ReadOnlySpan<char> s, IFormatProvider ^ provider) = ISpanParsable<System::Decimal>::Parse;
public static decimal Parse (ReadOnlySpan<char> s, IFormatProvider? provider);
static member Parse : ReadOnlySpan<char> * IFormatProvider -> decimal
Public Shared Function Parse (s As ReadOnlySpan(Of Char), provider As IFormatProvider) As Decimal

パラメーター

s
ReadOnlySpan<Char>

解析する文字のスパン。

provider
IFormatProvider

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

戻り値

を解析した s結果。

実装

適用対象

Parse(String, NumberStyles)

指定のスタイルで表現された数値の文字列形式を、それと等価な Decimal に変換します。

public:
 static System::Decimal Parse(System::String ^ s, System::Globalization::NumberStyles style);
public static decimal Parse (string s, System.Globalization.NumberStyles style);
static member Parse : string * System.Globalization.NumberStyles -> decimal
Public Shared Function Parse (s As String, style As NumberStyles) As Decimal

パラメーター

s
String

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

style
NumberStyles

s で使用可能なスタイル要素を示す、NumberStyles 値のビットごとの組み合わせ。 通常指定する値は、Number です。

戻り値

Decimal で指定されたとおりの s に格納されている数値と等価の style 数値。

例外

snullです。

styleNumberStyles 値ではありません。

または

styleAllowHexSpecifier 値です。

s が正しい形式ではありません。

sは、Decimal.MinValue より小さい数値または Decimal.MaxValue より大きい数値を表します

次のコード例では、 メソッドを Parse(String, NumberStyles) 使用して、en-US カルチャを使用して値の Decimal 文字列表現を解析します。

string value;
decimal number;
NumberStyles style;

// Parse string with a floating point value using NumberStyles.None.
value = "8694.12";
style = NumberStyles.None;
try
{
   number = Decimal.Parse(value, style);
   Console.WriteLine("'{0}' converted to {1}.", value, number);
}
catch (FormatException)
{
   Console.WriteLine("Unable to parse '{0}'.", value);
}
// Displays:
//    Unable to parse '8694.12'.

// Parse string with a floating point value and allow decimal point.
style = NumberStyles.AllowDecimalPoint;
number = Decimal.Parse(value, style);
Console.WriteLine("'{0}' converted to {1}.", value, number);
// Displays:
//    '8694.12' converted to 8694.12.

// Parse string with negative value in parentheses
value = "(1,789.34)";
style = NumberStyles.AllowDecimalPoint | NumberStyles.AllowThousands |
        NumberStyles.AllowParentheses;
number = Decimal.Parse(value, style);
Console.WriteLine("'{0}' converted to {1}.", value, number);
// Displays:
//    '(1,789.34)' converted to -1789.34.

// Parse string using Number style
value = " -17,623.49 ";
style = NumberStyles.Number;
number = Decimal.Parse(value, style);
Console.WriteLine("'{0}' converted to {1}.", value, number);
// Displays:
//    ' -17,623.49 ' converted to -17623.49.
// Parse string with a floating point value using NumberStyles.None.
let value = "8694.12"
let style = NumberStyles.None
try
    let number = Decimal.Parse(value, style)
    printfn $"'{value}' converted to {number}."
with :? FormatException ->
    printfn $"Unable to parse '{value}'."
// Displays:
//    Unable to parse '8694.12'.

// Parse string with a floating point value and allow decimal point.
let style = NumberStyles.AllowDecimalPoint
let number = Decimal.Parse(value, style)
printfn $"'{value}' converted to {number}."
// Displays:
//    '8694.12' converted to 8694.12.

// Parse string with negative value in parentheses
let value = "(1,789.34)"
let style = 
    NumberStyles.AllowDecimalPoint ||| 
    NumberStyles.AllowThousands ||| 
    NumberStyles.AllowParentheses
let number = Decimal.Parse(value, style)
printfn $"'{value}' converted to {number}."
// Displays:
//    '(1,789.34)' converted to -1789.34.

// Parse string using Number style
let value = " -17,623.49 "
let style = NumberStyles.Number
let number = Decimal.Parse(value, style)
printfn $"'{value}' converted to {number}."
// Displays:
//    ' -17,623.49 ' converted to -17623.49.
Dim value As String
Dim number As Decimal
Dim style As NumberStyles

' Parse string with a floating point value using NumberStyles.None. 
value = "8694.12"
style = NumberStyles.None
Try
   number = Decimal.Parse(value, style)  
   Console.WriteLine("'{0}' converted to {1}.", value, number)
Catch e As FormatException
   Console.WriteLine("Unable to parse '{0}'.", value)
End Try
' Displays:
'    Unable to parse '8694.12'.

' Parse string with a floating point value and allow decimal point. 
style = NumberStyles.AllowDecimalPoint
number = Decimal.Parse(value, style)  
Console.WriteLine("'{0}' converted to {1}.", value, number)
' Displays:
'    '8694.12' converted to 8694.12.

' Parse string with negative value in parentheses
value = "(1,789.34)"
style = NumberStyles.AllowDecimalPoint Or NumberStyles.AllowThousands Or _
        NumberStyles.AllowParentheses 
number = Decimal.Parse(value, style)  
Console.WriteLine("'{0}' converted to {1}.", value, number)
' Displays:
'    '(1,789.34)' converted to -1789.34.

' Parse string using Number style
value = " -17,623.49 "
style = NumberStyles.Number
number = Decimal.Parse(value, style)  
Console.WriteLine("'{0}' converted to {1}.", value, number)
' Displays:
'    ' -17,623.49 ' converted to -17623.49.

注釈

パラメーターは style 、解析操作を成功させるために パラメーターで s 許可されるスタイル要素 (桁区切り記号、空白、通貨記号など) を定義します。 列挙体のビット フラグ NumberStyles の組み合わせである必要があります。 次 NumberStyles のメンバーはサポートされていません。

style値に応じて、 パラメーターに s 次の要素を含めることができます。

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

角かっこ ([ および ]) 内の要素は省略可能です。 次の表は、それぞれの要素の説明です。

要素 説明
ws オプションの空白。 空白は、 フラグを含む場合は のs先頭に表示され、フラグが含NumberStyles.AllowLeadingWhiteまれている場合styleは のs末尾にNumberStyles.AllowTrailingWhite表示styleされます。
$ カルチャ固有の通貨記号。 文字列内での位置は、現在のカルチャの NumberFormatInfo.CurrencyNegativePattern プロパティと NumberFormatInfo.CurrencyPositivePattern プロパティによって定義されます。 に フラグが含まれている場合styleは、現在のカルチャの通貨記号を NumberStyles.AllowCurrencySymbols表示できます。
sign 省略可能な記号。 署名は、 フラグを含む場合は のs先頭に表示され、フラグが含NumberStyles.AllowLeadingSignまれている場合styleは のs末尾にNumberStyles.AllowTrailingSign表示styleされます。 に フラグが含まれている場合styleは、かっこを使用sして負の値をNumberStyles.AllowParentheses示すことができます。
数値 0 から 9 までの数字のシーケンス。
, カルチャ固有の桁区切り記号。 現在のカルチャの桁区切り記号は、 に フラグが含まれている場合stylesNumberStyles.AllowThousands表示できます。
. カルチャ固有の小数点記号。 に フラグが含まれている場合styleは、現在のカルチャの小数点記号を NumberStyles.AllowDecimalPoints表示できます。
小数部の桁数 0 から 9 までの数字のシーケンス。 小数部の数字は、 に s フラグがNumberStyles.AllowDecimalPoint含まれている場合styleにのみ表示されます。
e 値が指数表記で表されることを示す 'e' または 'E' 文字。 フラグが含まれている場合style、パラメーターはs指数表記で数値をNumberStyles.AllowExponent表すことができます。

注意

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

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

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

パラメーターは s 、現在のシステム カルチャ用に初期化されたオブジェクトの NumberFormatInfo 書式設定情報を使用して解析されます。 詳細については、「CurrentInfo」を参照してください。

には Decimal 29 桁の有効桁数があります。 が 29 桁を超えるが、小数部を持ち、 と のMaxValueMinValue範囲内にある数値を表す場合s、最も近い四捨五入を使用して、数値は切り捨てられず、29 桁に丸められます。

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

こちらもご覧ください

適用対象

Parse(String, IFormatProvider)

指定したカルチャ固有の書式情報を使用して、文字列形式の数値をそれと等価の Decimal に変換します。

public:
 static System::Decimal Parse(System::String ^ s, IFormatProvider ^ provider);
public:
 static System::Decimal Parse(System::String ^ s, IFormatProvider ^ provider) = IParsable<System::Decimal>::Parse;
public static decimal Parse (string s, IFormatProvider provider);
public static decimal Parse (string s, IFormatProvider? provider);
static member Parse : string * IFormatProvider -> decimal
Public Shared Function Parse (s As String, provider As IFormatProvider) As Decimal

パラメーター

s
String

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

provider
IFormatProvider

s に関するカルチャに固有の解析情報を提供する IFormatProvider

戻り値

Decimal で指定されたとおりの s に格納されている数値と等価の provider 数値。

実装

例外

snullです。

s が正しい形式ではありません。

s は、 Decimal.MinValue より小さい数値または Decimal.MaxValue より大きい数値を表します。

次の例は、Web フォームのボタン クリック イベント ハンドラーです。 プロパティによって返される配列を HttpRequest.UserLanguages 使用して、ユーザーのロケールを決定します。 その後、そのロケールに CultureInfo 対応する オブジェクトをインスタンス化します。 NumberFormatInfoそのCultureInfoオブジェクトに属する オブジェクトは、 メソッドにParse(String, IFormatProvider)渡され、ユーザーの入力を値にDecimal変換します。

protected void OkToDecimal_Click(object sender, EventArgs e)
{
    string locale;
    decimal number;
    CultureInfo culture;

    // Return if string is empty
    if (String.IsNullOrEmpty(this.inputNumber.Text))
        return;

    // Get locale of web request to determine possible format of number
    if (Request.UserLanguages.Length == 0)
        return;
    locale = Request.UserLanguages[0];
    if (String.IsNullOrEmpty(locale))
        return;

    // Instantiate CultureInfo object for the user's locale
    culture = new CultureInfo(locale);

    // Convert user input from a string to a number
    try
    {
        number = Decimal.Parse(this.inputNumber.Text, culture.NumberFormat);
    }
    catch (FormatException)
    {
        return;
    }
    catch (Exception)
    {
        return;
    }
    // Output number to label on web form
    this.outputNumber.Text = "Number is " + number.ToString();
}
Protected Sub OkToDecimal_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles OkToDecimal.Click
   Dim locale As String
   Dim culture As CultureInfo
   Dim number As Decimal

   ' Return if string is empty
   If String.IsNullOrEmpty(Me.inputNumber.Text) Then Exit Sub

   ' Get locale of web request to determine possible format of number
   If Request.UserLanguages.Length = 0 Then Exit Sub
   locale = Request.UserLanguages(0)
   If String.IsNullOrEmpty(locale) Then Exit Sub

   ' Instantiate CultureInfo object for the user's locale
   culture = New CultureInfo(locale)

   ' Convert user input from a string to a number
   Try
      number = Decimal.Parse(Me.inputNumber.Text, culture.NumberFormat)
   Catch ex As FormatException
      Exit Sub
   Catch ex As Exception
      Exit Sub
   End Try

   ' Output number to label on web form
   Me.outputNumber.Text = "Number is " & number.ToString()
End Sub

注釈

メソッドの Parse(String, IFormatProvider) このオーバーロードは、さまざまな方法で書式設定できるテキストを値に変換するために一般的に Decimal 使用されます。 たとえば、ユーザーが入力したテキストを HTML テキスト ボックスに数値に変換するために使用できます。

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

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

角かっこ ([ および ]) 内の要素は省略可能です。 次の表は、それぞれの要素の説明です。

要素 説明
ws オプションの空白。
sign 省略可能な記号。
数値 0 から 9 までの数字のシーケンス。
, カルチャ固有の桁区切り記号。
. カルチャ固有の小数点記号。
小数部の数字 0 から 9 までの数字のシーケンス。

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

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

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

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

こちらもご覧ください

適用対象

Parse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider)

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

public static decimal Parse (ReadOnlySpan<byte> utf8Text, System.Globalization.NumberStyles style = System.Globalization.NumberStyles.Number, IFormatProvider? provider = default);
static member Parse : ReadOnlySpan<byte> * System.Globalization.NumberStyles * IFormatProvider -> decimal
Public Shared Function Parse (utf8Text As ReadOnlySpan(Of Byte), Optional style As NumberStyles = System.Globalization.NumberStyles.Number, Optional provider As IFormatProvider = Nothing) As Decimal

パラメーター

utf8Text
ReadOnlySpan<Byte>

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

style
NumberStyles

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

provider
IFormatProvider

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

戻り値

を解析した utf8Text結果。

実装

適用対象

Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider)

指定したスタイルとカルチャ固有の書式を使用して、数値のスパン表現を等価の Decimal に変換します。

public static decimal Parse (ReadOnlySpan<char> s, System.Globalization.NumberStyles style = System.Globalization.NumberStyles.Number, IFormatProvider? provider = default);
public static decimal Parse (ReadOnlySpan<char> s, System.Globalization.NumberStyles style = System.Globalization.NumberStyles.Number, IFormatProvider provider = default);
static member Parse : ReadOnlySpan<char> * System.Globalization.NumberStyles * IFormatProvider -> decimal
Public Shared Function Parse (s As ReadOnlySpan(Of Char), Optional style As NumberStyles = System.Globalization.NumberStyles.Number, Optional provider As IFormatProvider = Nothing) As Decimal

パラメーター

s
ReadOnlySpan<Char>

変換する数値を表す文字を格納しているスパン。

style
NumberStyles

s で使用可能なスタイル要素を示す、NumberStyles 値のビットごとの組み合わせ。 通常指定する値は、Number です。

provider
IFormatProvider

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

戻り値

Decimals に指定されたとおり、style に格納されている数値と等価の provider 数値。

実装

適用対象

Parse(String, NumberStyles, IFormatProvider)

指定したスタイルとカルチャ固有の書式を使用して、文字列形式の数値をそれと等価の Decimal に変換します。

public:
 static System::Decimal Parse(System::String ^ s, System::Globalization::NumberStyles style, IFormatProvider ^ provider);
public:
 static System::Decimal Parse(System::String ^ s, System::Globalization::NumberStyles style, IFormatProvider ^ provider) = System::Numerics::INumberBase<System::Decimal>::Parse;
public static decimal Parse (string s, System.Globalization.NumberStyles style, IFormatProvider provider);
public static decimal Parse (string s, System.Globalization.NumberStyles style, IFormatProvider? provider);
static member Parse : string * System.Globalization.NumberStyles * IFormatProvider -> decimal
Public Shared Function Parse (s As String, style As NumberStyles, provider As IFormatProvider) As Decimal

パラメーター

s
String

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

style
NumberStyles

s で使用可能なスタイル要素を示す、NumberStyles 値のビットごとの組み合わせ。 通常指定する値は、Number です。

provider
IFormatProvider

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

戻り値

Decimals に指定されたとおり、style に格納されている数値と等価の provider 数値。

実装

例外

s が正しい形式ではありません。

s は、 Decimal.MinValue より小さい数値または Decimal.MaxValue より大きい数値を表します。

snullです。

styleNumberStyles 値ではありません。

または

styleAllowHexSpecifier 値です。

次の例では、さまざまな style パラメーターと provider パラメーターを使用して、値の Decimal 文字列表現を解析します。

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

// Parse string using " " as the thousands separator
// and "," as the decimal separator for fr-FR culture.
value = "892 694,12";
style = NumberStyles.AllowDecimalPoint | NumberStyles.AllowThousands;
provider = new CultureInfo("fr-FR");

number = Decimal.Parse(value, style, provider);
Console.WriteLine("'{0}' converted to {1}.", value, number);
// Displays:
//    '892 694,12' converted to 892694.12.

try
{
   number = Decimal.Parse(value, style, CultureInfo.InvariantCulture);
   Console.WriteLine("'{0}' converted to {1}.", value, number);
}
catch (FormatException)
{
   Console.WriteLine("Unable to parse '{0}'.", value);
}
// Displays:
//    Unable to parse '892 694,12'.

// Parse string using "$" as the currency symbol for en-GB and
// en-US cultures.
value = "$6,032.51";
style = NumberStyles.Number | NumberStyles.AllowCurrencySymbol;
provider = new CultureInfo("en-GB");

try
{
   number = Decimal.Parse(value, style, provider);
   Console.WriteLine("'{0}' converted to {1}.", value, number);
}
catch (FormatException)
{
   Console.WriteLine("Unable to parse '{0}'.", value);
}
// Displays:
//    Unable to parse '$6,032.51'.

provider = new CultureInfo("en-US");
number = Decimal.Parse(value, style, provider);
Console.WriteLine("'{0}' converted to {1}.", value, number);
// Displays:
//    '$6,032.51' converted to 6032.51.
// Parse string using " " as the thousands separator
// and "," as the decimal separator for fr-FR culture.
let value = "892 694,12"
let style = NumberStyles.AllowDecimalPoint ||| NumberStyles.AllowThousands
let provider = CultureInfo "fr-FR"

let number = Decimal.Parse(value, style, provider)
printfn $"'{value}' converted to {number}."
// Displays:
//    '892 694,12' converted to 892694.12.

try
    let number = Decimal.Parse(value, style, CultureInfo.InvariantCulture)
    printfn $"'{value}' converted to {number}."
with :? FormatException ->
    printfn $"Unable to parse '{value}'."
// Displays:
//    Unable to parse '892 694,12'.

// Parse string using "$" as the currency symbol for en-GB and
// en-US cultures.
let value = "$6,032.51"
let style = NumberStyles.Number ||| NumberStyles.AllowCurrencySymbol
let provider = CultureInfo "en-GB"

try
    let number = Decimal.Parse(value, style, provider)
    printfn $"'{value}' converted to {number}."
with :? FormatException ->
    printfn $"Unable to parse '{value}'."
// Displays:
//    Unable to parse '$6,032.51'.

let provider = CultureInfo "en-US"
let number = Decimal.Parse(value, style, provider)
printfn $"'{value}' converted to {number}."
// Displays:
//    '$6,032.51' converted to 6032.51.
Dim value As String
Dim number As Decimal
Dim style As NumberStyles
Dim provider As CultureInfo

' Parse string using " " as the thousands separator 
' and "," as the decimal separator for fr-FR culture.
value = "892 694,12"
style = NumberStyles.AllowDecimalPoint Or NumberStyles.AllowThousands
provider = New CultureInfo("fr-FR")

number = Decimal.Parse(value, style, provider)  
Console.WriteLine("'{0}' converted to {1}.", value, number)
' Displays: 
'    '892 694,12' converted to 892694.12.

Try
   number = Decimal.Parse(value, style, CultureInfo.InvariantCulture)  
   Console.WriteLine("'{0}' converted to {1}.", value, number)
Catch e As FormatException
   Console.WriteLine("Unable to parse '{0}'.", value)
End Try
' Displays: 
'    Unable to parse '892 694,12'.  

' Parse string using "$" as the currency symbol for en-GB and
' en-US cultures.
value = "$6,032.51"
style = NumberStyles.Number Or NumberStyles.AllowCurrencySymbol
provider = New CultureInfo("en-GB")

Try
   number = Decimal.Parse(value, style, provider)  
   Console.WriteLine("'{0}' converted to {1}.", value, number)
Catch e As FormatException
   Console.WriteLine("Unable to parse '{0}'.", value)
End Try
' Displays: 
'    Unable to parse '$6,032.51'.

provider = New CultureInfo("en-US")
number = Decimal.Parse(value, style, provider)  
Console.WriteLine("'{0}' converted to {1}.", value, number)
' Displays: 
'    '$6,032.51' converted to 6032.51.

注釈

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

style値に応じて、 パラメーターに s 次の要素を含めることができます。

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

角かっこ ([ および ]) 内の要素は省略可能です。 次の表は、それぞれの要素の説明です。

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

Note

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

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

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

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

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

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

こちらもご覧ください

適用対象