次の方法で共有


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.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 が正しい形式ではありません。

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 省略可能な空白。
サイン 省略可能な記号。
0 から 9 までの数字のシーケンス。
, カルチャ固有の桁区切り記号。
. カルチャ固有の小数点記号。
fractional-digits 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 桁を超え、小数部を持ち、MaxValueMinValueの範囲内にある数値を表す場合、最も近い値に丸めて使用すると、数値は切り捨てられず、29 桁に丸められます。

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

こちらもご覧ください

適用対象

Parse(ReadOnlySpan<Byte>, IFormatProvider)

ソース:
Decimal.cs
ソース:
Decimal.cs
ソース:
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
ソース:
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.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 が正しい形式ではありません。

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

手記

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

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

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

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

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

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

こちらもご覧ください

適用対象

Parse(String, IFormatProvider)

ソース:
Decimal.cs
ソース:
Decimal.cs
ソース:
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 が正しい形式ではありません。

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 省略可能な空白。
サイン 省略可能な記号。
0 から 9 までの数字のシーケンス。
, カルチャ固有の桁区切り記号。
. カルチャ固有の小数点記号。
fractional-digits 0 から 9 までの数字のシーケンス。

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

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

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

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

こちらもご覧ください

適用対象

Parse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider)

ソース:
Decimal.cs
ソース:
Decimal.cs
ソース:
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.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.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 が正しい形式ではありません。

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

手記

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

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

NumberStyles 値 数字に加えて s で許可される要素
None digits 要素のみ。
AllowDecimalPoint .および小数部の要素
AllowExponent s パラメーターでは指数表記を使用することもできます。 このフラグは、フォーム の数字 E 桁の値をサポートします。正符号や負符号、小数点記号などの要素を含む文字列を正常に解析するには、追加のフラグが必要です。
AllowLeadingWhite sの先頭にある ws 要素。
AllowTrailingWhite の末尾にある s 要素。
AllowLeadingSign sの先頭にある sign 要素。
AllowTrailingSign の末尾にある s 要素。
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 桁を超え、小数部を持ち、MaxValueMinValueの範囲内にある数値を表す場合、最も近い値に丸めて使用すると、数値は切り捨てられず、29 桁に丸められます。

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

こちらもご覧ください

適用対象