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.cs
ソース:
Decimal.cs
ソース:
Decimal.cs

数値の文字列形式を等価の 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 が正しい形式ではありません。

は、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, IFormatProvider) メソッドまたは Decimal.Parse(String, NumberStyles, IFormatProvider) メソッドを使用します。

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

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

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

こちらもご覧ください

適用対象

Parse(ReadOnlySpan<Byte>, IFormatProvider)

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

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)

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

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

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.cs
ソース:
Decimal.cs
ソース:
Decimal.cs

指定したスタイルの数値の文字列形式を等価の 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です。

戻り値

styleで指定された s に含まれる数値に相当する Decimal 番号。

例外

snullです。

styleNumberStyles 値ではありません。

-又は-

styleAllowHexSpecifier 値です。

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

手記

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

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

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

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

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

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

こちらもご覧ください

適用対象

Parse(String, IFormatProvider)

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

指定したカルチャ固有の書式情報を使用して、数値の文字列形式を等価の 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

戻り値

providerで指定された s に含まれる数値に相当する Decimal 番号。

実装

例外

snullです。

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

は、Decimal.MinValue 未満の数値、または Decimal.MaxValueより大きい数値を表します。

次の例は、Web フォームのボタン クリック イベント ハンドラーです。 HttpRequest.UserLanguages プロパティによって返される配列を使用して、ユーザーのロケールを決定します。 その後、そのロケールに対応する CultureInfo オブジェクトをインスタンス化します。 その CultureInfo オブジェクトに属する NumberFormatInfo オブジェクトが 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) メソッドを使用します。

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

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

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

こちらもご覧ください

適用対象

Parse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider)

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

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.cs
ソース:
Decimal.cs
ソース:
Decimal.cs

指定したスタイルとカルチャ固有の形式を使用して、数値のスパン表現を同等の 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 オブジェクト。

戻り値

style および providerで指定された s に含まれる数値と等価の Decimal 番号。

実装

適用対象

Parse(String, NumberStyles, IFormatProvider)

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

指定したスタイルとカルチャ固有の形式を使用して、数値の文字列形式を等価の 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 オブジェクト。

戻り値

style および providerで指定された s に含まれる数値と等価の Decimal 番号。

実装

例外

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]

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

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

手記

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

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

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

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

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

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

こちらもご覧ください

適用対象