Decimal.Parse メソッド
定義
オーバーロード
Parse(String, NumberStyles, IFormatProvider) |
指定したスタイルとカルチャ固有の書式を使用して、文字列形式の数値をそれと等価の Decimal に変換します。Converts the string representation of a number to its Decimal equivalent using the specified style and culture-specific format. |
Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider) |
指定したスタイルとカルチャ固有の書式を使用して、数値のスパン表現を等価の Decimal に変換します。Converts the span representation of a number to its Decimal equivalent using the specified style and culture-specific format. |
Parse(String, IFormatProvider) |
指定したカルチャ固有の書式情報を使用して、文字列形式の数値をそれと等価の Decimal に変換します。Converts the string representation of a number to its Decimal equivalent using the specified culture-specific format information. |
Parse(String, NumberStyles) |
指定のスタイルで表現された数値の文字列形式を、それと等価な Decimal に変換します。Converts the string representation of a number in a specified style to its Decimal equivalent. |
Parse(String) |
数値の文字列形式を、それと等価の Decimal に変換します。Converts the string representation of a number to its Decimal equivalent. |
Parse(String, NumberStyles, IFormatProvider)
public:
static System::Decimal Parse(System::String ^ s, System::Globalization::NumberStyles style, IFormatProvider ^ provider);
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
変換する数値の文字列形式。The string representation of the number to convert.
- style
- NumberStyles
s
で使用可能なスタイル要素を示す、NumberStyles 値のビットごとの組み合わせ。A bitwise combination of NumberStyles values that indicates the style elements that can be present in s
. 通常指定する値は、Number です。A typical value to specify is Number.
- provider
- IFormatProvider
s
の書式設定に関するカルチャ固有の情報を提供する IFormatProvider オブジェクト。An IFormatProvider object that supplies culture-specific information about the format of s
.
戻り値
Decimal と s
に指定されたとおり、style
に格納されている数値と等価の provider
数値。The Decimal number equivalent to the number contained in s
as specified by style
and provider
.
例外
s
が正しい形式ではありません。s
is not in the correct format.
s
は、MinValue 未満の数値か、MaxValue より大きい数値を表します。s
represents a number less than MinValue or greater than MaxValue.
s
が null
です。s
is null
.
style
が NumberStyles 値ではありません。style
is not a NumberStyles value.
- または --or-
style
は AllowHexSpecifier 値です。style
is the AllowHexSpecifier value.
例
次の例では、さまざまなパラメーターを使用して、 style
provider
値の文字列形式を解析し Decimal ます。The following example uses a variety of style
and provider
parameters to parse the string representations of Decimal values.
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.
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
ます。The style
parameter defines the allowable format of the s
parameter for the parse operation to succeed. 列挙体のビットフラグの組み合わせである必要があり NumberStyles ます。It must be a combination of bit flags from the NumberStyles enumeration. 次の NumberStyles メンバーはサポートされていません。The following NumberStyles members are not supported:
の値に応じて style
、パラメーターには s
次の要素を含めることができます。Depending on the value of style
, the s
parameter may include the following elements:
jax-ws[$]シャープ[数字,] 数字 [. 小数部の桁] [e [符号] 数字] [ws][ws][$][sign][digits,]digits[.fractional-digits][e[sign]digits][ws]
角かっこ ([ および ]) 内の要素は省略可能です。Elements in square brackets ([ and ]) are optional. 次の表は、それぞれの要素の説明です。The following table describes each element.
要素Element | 説明Description |
---|---|
$ | カルチャ固有の通貨記号。A culture-specific currency symbol. 文字列内での位置は、 CurrencyNegativePattern CurrencyPositivePattern NumberFormatInfo パラメーターのメソッドによって返されるオブジェクトのプロパティとプロパティによって定義され GetFormat provider ます。Its position in the string is defined by the CurrencyNegativePattern and CurrencyPositivePattern properties of the NumberFormatInfo object returned by the GetFormat method of the provider parameter. に s フラグが含まれている場合は、通貨記号がに表示され style NumberStyles.AllowCurrencySymbol ます。The currency symbol can appear in s if style includes the NumberStyles.AllowCurrencySymbol flag. |
wsws | 省略可能な空白。Optional white space. がフラグを含む場合は、の先頭に空白が表示され s style ます。また、にフラグが含まれている場合は、 NumberStyles.AllowLeadingWhite の末尾に空白文字を含めることができ s style NumberStyles.AllowTrailingWhite ます。White space can appear at the beginning of s if style includes the NumberStyles.AllowLeadingWhite flag, and it can appear at the end of s if style includes the NumberStyles.AllowTrailingWhite flag. |
signsign | 省略可能な符号。An optional sign. にフラグが含まれている場合は、の先頭に符号が表示され s style ます。また、にフラグが含まれている場合は、 NumberStyles.AllowLeadingSign の末尾にも表示され s style NumberStyles.AllowTrailingSign ます。The sign can appear at the beginning of s if style includes the NumberStyles.AllowLeadingSign flag, and it can appear at the end of s if style includes the NumberStyles.AllowTrailingSign flag. に s フラグが含まれている場合は、かっこを使用して負の値を示すことができ style NumberStyles.AllowParentheses ます。Parentheses can be used in s to indicate a negative value if style includes the NumberStyles.AllowParentheses flag. |
数値digits | 0から9までの一連の数字。A sequence of digits ranging from 0 to 9. |
、, | カルチャ固有の桁区切り記号。A culture-specific thousands separator symbol. で定義されているカルチャの桁区切り記号は、 provider にフラグが含まれている場合にで使用でき s style NumberStyles.AllowThousands ます。The thousands separator of the culture defined by provider can appear in s if style includes the NumberStyles.AllowThousands flag. |
.. | カルチャ固有の小数点の記号。A culture-specific decimal point symbol. によって定義されたカルチャの小数点の記号は、 provider にフラグが含まれている場合にで使用でき s style NumberStyles.AllowDecimalPoint ます。The decimal point symbol of the culture defined by provider can appear in s if style includes the NumberStyles.AllowDecimalPoint flag. |
小数点以下桁数fractional-digits | 0から9までの一連の数字。A sequence of digits ranging from 0 to 9. にフラグが含まれている場合にのみ、に小数点以下の桁数が表示され s style NumberStyles.AllowDecimalPoint ます。Fractional digits can appear in s only if style includes the NumberStyles.AllowDecimalPoint flag. |
ee | "E" または "E" 文字。値が指数表記で表されることを示します。The 'e' or 'E' character, which indicates that the value is represented in exponential notation. に s フラグが含まれている場合、パラメーターは指数表記で数値を表すことができ style NumberStyles.AllowExponent ます。The s parameter can represent a number in exponential notation if style includes the NumberStyles.AllowExponent flag. |
注意
の終端の NUL (U + 0000) 文字 s
は、引数の値に関係なく、解析操作によって無視され style
ます。Any terminating NUL (U+0000) characters in s
are ignored by the parsing operation, regardless of the value of the style
argument.
数字のみを含む文字列 (スタイルに対応する None ) は、型の範囲内にある場合は、常に正常に解析され Decimal ます。A string with digits only (which corresponds to the None style) always parses successfully if it is in the range of the Decimal type. 残りの NumberStyles メンバーは、入力文字列に存在する必要があるが必須ではない要素を制御します。The remaining NumberStyles members control elements that may be but are not required to be present in the input string. 次の表は、の各メンバーが、に存在する可能性のある要素にどのように影響するかを示して NumberStyles s
います。The following table indicates how individual NumberStyles members affect the elements that may be present in s
.
NumberStyles 値NumberStyles value | 数字に加えてで許可される要素Elements permitted in s in addition to digits |
---|---|
None | Digits 要素のみ。The digits element only. |
AllowDecimalPoint | .The . と 小数部の桁 の要素。and fractional-digits elements. |
AllowExponent | パラメーターでは、 s 指数表記を使用することもできます。The s parameter can also use exponential notation. このフラグは、 数字 形式の値をサポートして います。 正または負の記号や小数点の記号などの要素を使用して文字列を正常に解析するには、追加のフラグが必要です。This flag supports values in the form digits E digits; additional flags are needed to successfully parse strings with elements such as positive or negative signs and decimal point symbols. |
AllowLeadingWhite | の先頭にある ws 要素 s 。The ws element at the beginning of s . |
AllowTrailingWhite | の末尾の ws 要素 s 。The ws element at the end of s . |
AllowLeadingSign | の先頭にある sign 要素 s 。The sign element at the beginning of s . |
AllowTrailingSign | の末尾の sign 要素 s 。The sign element at the end of s . |
AllowParentheses | 数値を囲むかっこ形式の sign 要素。The sign element in the form of parentheses enclosing the numeric value. |
AllowThousands | 要素 、 要素。The , element. |
AllowCurrencySymbol | $ 要素。The $ element. |
Currency | すべて。All. パラメーターは、 s 16 進数または指数表記の数値を表すことはできません。The s parameter cannot represent a hexadecimal number or a number in exponential notation. |
Float | の先頭または末尾にある ws 要素 s 、 sign の先頭にある、 s および 。The ws element at the beginning or end of s , sign at the beginning of s , and the . 表す.symbol. パラメーターでは、 s 指数表記を使用することもできます。The s parameter can also use exponential notation. |
Number | 、 ws 、 sign 、、および 。The ws , sign , ,, and . 要素でサポートされます。elements. |
Any | を除くすべてのスタイルは、 s 16 進数を表すことはできません。All styles, except s cannot represent a hexadecimal number. |
パラメーターは、 provider
IFormatProvider NumberFormatInfo オブジェクトやオブジェクトなどの実装です CultureInfo 。The provider
parameter is an IFormatProvider implementation, such as a NumberFormatInfo or CultureInfo object. パラメーターは、 provider
解析に使用されるカルチャ固有の情報を提供します。The provider
parameter supplies culture-specific information used in parsing. provider
が null
の場合は、スレッドの現在のカルチャが使用されます。If provider
is null
, the thread current culture is used.
Decimalオブジェクトの有効桁数は29桁です。A Decimal object has 29 digits of precision. が s
29 桁を超える数値を表していて、小数部を持ち、かつの範囲内にある場合、数値は切り捨てられず、切り捨てられずに、 MaxValue MinValue 近似値に丸められて29桁に丸められます。If s
represents a number that has more than 29 digits, but has a fractional part and is within the range of MaxValue and MinValue, the number is rounded, not truncated, to 29 digits using rounding to nearest.
解析操作中にパラメーターで区切り記号が検出され、 s
該当する通貨または数値の10進数と桁区切り記号が同じ場合、解析操作では、区切り記号が桁区切り記号ではなく小数点であると見なされます。If a separator is encountered in the s
parameter during a parse operation, and the applicable currency or number decimal and group separators are the same, the parse operation assumes that the separator is a decimal separator rather than a group separator. 区切り記号の詳細については、「」、「」、「」、および「」を参照してください CurrencyDecimalSeparator NumberDecimalSeparator CurrencyGroupSeparator NumberGroupSeparator 。For more information about separators, see CurrencyDecimalSeparator, NumberDecimalSeparator, CurrencyGroupSeparator, and NumberGroupSeparator.
こちらもご覧ください
適用対象
Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider)
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>
変換する数値を表す文字を格納しているスパン。The span containing the characters representing the number to convert.
- style
- NumberStyles
s
で使用可能なスタイル要素を示す、NumberStyles 値のビットごとの組み合わせ。A bitwise combination of NumberStyles values that indicates the style elements that can be present in s
. 通常指定する値は、Number です。A typical value to specify is Number.
- provider
- IFormatProvider
s
の書式設定に関するカルチャ固有の情報を提供する IFormatProvider オブジェクト。An IFormatProvider object that supplies culture-specific information about the format of s
.
戻り値
Decimal と s
に指定されたとおり、style
に格納されている数値と等価の provider
数値。The Decimal number equivalent to the number contained in s
as specified by style
and provider
.
適用対象
Parse(String, IFormatProvider)
public:
static System::Decimal Parse(System::String ^ s, IFormatProvider ^ provider);
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
変換する数値の文字列形式。The string representation of the number to convert.
- provider
- IFormatProvider
s
に関するカルチャに固有の解析情報を提供する IFormatProvider。An IFormatProvider that supplies culture-specific parsing information about s
.
戻り値
Decimal で指定されたとおりの s
に格納されている数値と等価の provider
数値。The Decimal number equivalent to the number contained in s
as specified by provider
.
例外
s
が null
です。s
is null
.
s
が正しい形式ではありません。s
is not of the correct format.
s
は、MinValue 未満の数値か、MaxValue より大きい数値を表します。s
represents a number less than MinValue or greater than MaxValue.
例
次の例は、Web フォームのボタンクリックイベントハンドラーです。The following example is the button click event handler of a Web form. このメソッドは、プロパティによって返される配列を使用して、 HttpRequest.UserLanguages ユーザーのロケールを決定します。It uses the array returned by the HttpRequest.UserLanguages property to determine the user's locale. 次に、その CultureInfo ロケールに対応するオブジェクトをインスタンス化します。It then instantiates a CultureInfo object that corresponds to that locale. 次に、 NumberFormatInfo そのオブジェクトに属するオブジェクトを CultureInfo メソッドに渡して、 Parse(String, IFormatProvider) ユーザーの入力を値に変換し Decimal ます。The NumberFormatInfo object that belongs to that CultureInfo object is then passed to the Parse(String, IFormatProvider) method to convert the user's input to a Decimal value.
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 ます。This overload of the Parse(String, IFormatProvider) method is commonly used to convert text that can be formatted in a variety of ways to a Decimal value. たとえば、ユーザーが入力したテキストを HTML テキストボックスに数値に変換するために使用できます。For example, it can be used to convert the text entered by a user into an HTML text box to a numeric value.
パラメーターには、 s
次のような形式の数値が含まれています。The s
parameter contains a number of the form:
jax-wsシャープ[数字,] 数字 [. 小数部の桁] [ws][ws][sign][digits,]digits[.fractional-digits][ws]
角かっこ ([ および ]) 内の要素は省略可能です。Elements in square brackets ([ and ]) are optional. 次の表は、それぞれの要素の説明です。The following table describes each element.
要素Element | 説明Description |
---|---|
wsws | 省略可能な空白。Optional white space. |
signsign | 省略可能な符号。An optional sign. |
数値digits | 0から9までの一連の数字。A sequence of digits ranging from 0 to 9. |
、, | カルチャ固有の桁区切り記号。A culture-specific thousands separator symbol. |
.. | カルチャ固有の小数点の記号。A culture-specific decimal point symbol. |
小数点以下桁数fractional-digits | 0から9までの一連の数字。A sequence of digits ranging from 0 to 9. |
s
パラメーターは、スタイルを使用して解釈され NumberStyles.Number ます。The s
parameter is interpreted using the NumberStyles.Number style. つまり、空白と桁区切り記号は許可されますが、通貨記号は使用できません。This means that white space and thousands separators are allowed but currency symbols are not. に含まれる要素 (通貨記号、桁区切り記号、空白文字など) を明示的に定義するには s
、メソッドを使用し Decimal.Parse(String, NumberStyles, IFormatProvider) ます。To explicitly define the elements (such as currency symbols, thousands separators, and white space) that can be present in s
, use the Decimal.Parse(String, NumberStyles, IFormatProvider) method.
パラメーターは、 provider
IFormatProvider NumberFormatInfo オブジェクトやオブジェクトなどの実装です CultureInfo 。The provider
parameter is an IFormatProvider implementation, such as a NumberFormatInfo or CultureInfo object. パラメーターは、 provider
解析に使用されるカルチャ固有の情報を提供します。The provider
parameter supplies culture-specific information used in parsing. provider
が null
の場合は、スレッドの現在のカルチャが使用されます。If provider
is null
, the thread current culture is used.
Decimalオブジェクトの有効桁数は29桁です。A Decimal object has 29 digits of precision. が s
29 桁を超える数値を表していて、小数部を持ち、かつの範囲内にある場合、数値は切り捨てられず、切り捨てられずに、 MaxValue MinValue 近似値に丸められて29桁に丸められます。If s
represents a number that has more than 29 digits, but has a fractional part and is within the range of MaxValue and MinValue, the number is rounded, not truncated, to 29 digits using rounding to nearest.
解析操作中にパラメーターで区切り記号が検出され、 s
該当する通貨または数値の10進数と桁区切り記号が同じ場合、解析操作では、区切り記号が桁区切り記号ではなく小数点であると見なされます。If a separator is encountered in the s
parameter during a parse operation, and the applicable currency or number decimal and group separators are the same, the parse operation assumes that the separator is a decimal separator rather than a group separator. 区切り記号の詳細については、「」、「」、「」、および「」を参照してください CurrencyDecimalSeparator NumberDecimalSeparator CurrencyGroupSeparator NumberGroupSeparator 。For more information about separators, see CurrencyDecimalSeparator, NumberDecimalSeparator, CurrencyGroupSeparator, and NumberGroupSeparator.
こちらもご覧ください
適用対象
Parse(String, NumberStyles)
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
変換する数値の文字列形式。The string representation of the number to convert.
- style
- NumberStyles
s
で使用可能なスタイル要素を示す、NumberStyles 値のビットごとの組み合わせ。A bitwise combination of NumberStyles values that indicates the style elements that can be present in s
. 通常指定する値は、Number です。A typical value to specify is Number.
戻り値
Decimal で指定されたとおりの s
に格納されている数値と等価の style
数値。The Decimal number equivalent to the number contained in s
as specified by style
.
例外
s
が null
です。s
is null
.
style
が NumberStyles 値ではありません。style
is not a NumberStyles value.
- または --or-
style
は AllowHexSpecifier 値です。style
is the AllowHexSpecifier value.
s
が正しい形式ではありません。s
is not in the correct format.
s
は、MinValue 未満の数値か、MaxValue より大きい数値を表します。s
represents a number less than MinValue or greater than MaxValue
例
次のコード例では、メソッドを使用し Parse(String, NumberStyles) て、 Decimal en-us カルチャを使用して値の文字列形式を解析します。The following code example uses the Parse(String, NumberStyles) method to parse the string representations of Decimal values using the en-US culture.
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.
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
ます。The style
parameter defines the style elements (such as thousands separators, white space, and currency symbols) that are allowed in the s
parameter for the parse operation to succeed. 列挙体のビットフラグの組み合わせである必要があり NumberStyles ます。It must be a combination of bit flags from the NumberStyles enumeration. 次の NumberStyles メンバーはサポートされていません。The following NumberStyles members are not supported:
の値に応じて style
、パラメーターには s
次の要素を含めることができます。Depending on the value of style
, the s
parameter may include the following elements:
jax-ws[$]シャープ[数字,] 数字 [. 小数部の桁] [e [符号] 数字] [ws][ws][$][sign][digits,]digits[.fractional-digits][e[sign]digits][ws]
角かっこ ([ および ]) 内の要素は省略可能です。Elements in square brackets ([ and ]) are optional. 次の表は、それぞれの要素の説明です。The following table describes each element.
要素Element | 説明Description |
---|---|
wsws | 省略可能な空白。Optional white space. がフラグを含む場合は、の先頭に空白が表示され s style ます。また、にフラグが含まれている場合は、 NumberStyles.AllowLeadingWhite の末尾に空白文字を含めることができ s style NumberStyles.AllowTrailingWhite ます。White space can appear at the beginning of s if style includes the NumberStyles.AllowLeadingWhite flag, and it can appear at the end of s if style includes the NumberStyles.AllowTrailingWhite flag. |
$ | カルチャ固有の通貨記号。A culture-specific currency symbol. 文字列内での位置は、現在の NumberFormatInfo.CurrencyNegativePattern カルチャのプロパティとプロパティによって定義され NumberFormatInfo.CurrencyPositivePattern ます。Its position in the string is defined by the NumberFormatInfo.CurrencyNegativePattern and NumberFormatInfo.CurrencyPositivePattern properties of the current culture. に s フラグが含まれている場合は、現在のカルチャの通貨記号がに表示され style NumberStyles.AllowCurrencySymbol ます。The current culture's currency symbol can appear in s if style includes the NumberStyles.AllowCurrencySymbol flag. |
signsign | 省略可能な符号。An optional sign. にフラグが含まれている場合は、の先頭に符号が表示され s style ます。また、にフラグが含まれている場合は、 NumberStyles.AllowLeadingSign の末尾にも表示され s style NumberStyles.AllowTrailingSign ます。The sign can appear at the beginning of s if style includes the NumberStyles.AllowLeadingSign flag, and it can appear at the end of s if style includes the NumberStyles.AllowTrailingSign flag. に s フラグが含まれている場合は、かっこを使用して負の値を示すことができ style NumberStyles.AllowParentheses ます。Parentheses can be used in s to indicate a negative value if style includes the NumberStyles.AllowParentheses flag. |
数値digits | 0から9までの一連の数字。A sequence of digits ranging from 0 to 9. |
、, | カルチャ固有の桁区切り記号。A culture-specific thousands separator symbol. に s フラグが含まれている場合、現在のカルチャの桁区切り記号がに表示され style NumberStyles.AllowThousands ます。The current culture's thousands separator can appear in s if style includes the NumberStyles.AllowThousands flag. |
.. | カルチャ固有の小数点の記号。A culture-specific decimal point symbol. に s フラグが含まれている場合、現在のカルチャの小数点の記号がに表示され style NumberStyles.AllowDecimalPoint ます。The current culture's decimal point symbol can appear in s if style includes the NumberStyles.AllowDecimalPoint flag. |
小数点以下桁数fractional-digits | 0から9までの一連の数字。A sequence of digits ranging from 0 to 9. にフラグが含まれている場合にのみ、に小数点以下の桁数が表示され s style NumberStyles.AllowDecimalPoint ます。Fractional digits can appear in s only if style includes the NumberStyles.AllowDecimalPoint flag. |
ee | "E" または "E" 文字。値が指数表記で表されることを示します。The 'e' or 'E' character, which indicates that the value is represented in exponential notation. に s フラグが含まれている場合、パラメーターは指数表記で数値を表すことができ style NumberStyles.AllowExponent ます。The s parameter can represent a number in exponential notation if style includes the NumberStyles.AllowExponent flag. |
注意
の終端の NUL (U + 0000) 文字 s
は、引数の値に関係なく、解析操作によって無視され style
ます。Any terminating NUL (U+0000) characters in s
are ignored by the parsing operation, regardless of the value of the style
argument.
数字のみを含む文字列 (スタイルに対応する None ) は、型の範囲内にある場合は、常に正常に解析され Decimal ます。A string with digits only (which corresponds to the None style) always parses successfully if it is in the range of the Decimal type. 残りの NumberStyles メンバーは、入力文字列に存在する必要があるが必須ではない要素を制御します。The remaining NumberStyles members control elements that may be but are not required to be present in the input string. 次の表は、の各メンバーが、に存在する可能性のある要素にどのように影響するかを示して NumberStyles s
います。The following table indicates how individual NumberStyles members affect the elements that may be present in s
.
NumberStyles 値NumberStyles value | 数字に加えてで許可される要素Elements permitted in s in addition to digits |
---|---|
None | Digits 要素のみ。The digits element only. |
AllowDecimalPoint | .The . と 小数部の桁 の要素。and fractional-digits elements. |
AllowExponent | パラメーターでは、 s 指数表記を使用することもできます。The s parameter can also use exponential notation. このフラグは、 数字 形式の値をサポートして います。 正または負の記号や小数点の記号などの要素を使用して文字列を正常に解析するには、追加のフラグが必要です。This flag supports values in the form digits E digits; additional flags are needed to successfully parse strings with elements such as positive or negative signs and decimal point symbols. |
AllowLeadingWhite | の先頭にある ws 要素 s 。The ws element at the beginning of s . |
AllowTrailingWhite | の末尾の ws 要素 s 。The ws element at the end of s . |
AllowLeadingSign | の先頭にある sign 要素 s 。The sign element at the beginning of s . |
AllowTrailingSign | の末尾の sign 要素 s 。The sign element at the end of s . |
AllowParentheses | 数値を囲むかっこ形式の sign 要素。The sign element in the form of parentheses enclosing the numeric value. |
AllowThousands | 要素 、 要素。The , element. |
AllowCurrencySymbol | $ 要素。The $ element. |
Currency | すべて。All. パラメーターは、 s 16 進数または指数表記の数値を表すことはできません。The s parameter cannot represent a hexadecimal number or a number in exponential notation. |
Float | の先頭または末尾にある ws 要素、 s の先頭に 符号 、 s および . 記号。The ws element at the beginning or end of s , sign at the beginning of s , and the . symbol. パラメーターでは、 s 指数表記を使用することもできます。The s parameter can also use exponential notation. |
Number | 、 ws sign 、およびの各 , . 要素。The ws , sign , , and . elements. |
Any | を除くすべてのスタイルは、 s 16 進数を表すことはできません。All styles, except s cannot represent a hexadecimal number. |
s
パラメーターは、 NumberFormatInfo 現在のシステムカルチャに対して初期化されたオブジェクトの書式情報を使用して解析されます。The s
parameter is parsed using the formatting information in a NumberFormatInfo object initialized for the current system culture. 詳細については、「CurrentInfo」を参照してください。For more information, see CurrentInfo.
の Decimal 有効桁数は29桁です。A Decimal has 29 digits of precision. が s
29 桁を超える数値を表していて、小数部を持ち、かつの範囲内にある場合、数値は切り捨てられず、切り捨てられずに、 MaxValue MinValue 近似値に丸められて29桁に丸められます。If s
represents a number that has more than 29 digits, but has a fractional part and is within the range of MaxValue and MinValue, the number is rounded, not truncated, to 29 digits using rounding to nearest.
解析操作中にパラメーターで区切り記号が検出された場合 s
、には styles
との値が含まれ、該当する通貨または数値の10進数と桁区切り記号は同じであるため、解析操作では、区切り記号が桁区切り記号では NumberStyles.AllowThousands NumberStyles.AllowDecimalPoint なく小数点であると見なされます。If a separator is encountered in the s
parameter during a parse operation, styles
includes the NumberStyles.AllowThousands and NumberStyles.AllowDecimalPoint values, and the applicable currency or number decimal and group separators are the same, the parse operation assumes that the separator is a decimal separator rather than a group separator. 区切り記号の詳細については、「」、「」、「」、および「」を参照してください CurrencyDecimalSeparator NumberDecimalSeparator CurrencyGroupSeparator NumberGroupSeparator 。For more information about separators, see CurrencyDecimalSeparator, NumberDecimalSeparator, CurrencyGroupSeparator, and NumberGroupSeparator.
こちらもご覧ください
適用対象
Parse(String)
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
変換する数値の文字列形式。The string representation of the number to convert.
戻り値
s
に含まれている数値と等価。The equivalent to the number contained in s
.
例外
s
が null
です。s
is null
.
s
が正しい形式ではありません。s
is not in the correct format.
s
は、MinValue 未満の数値か、MaxValue より大きい数値を表します。s
represents a number less than MinValue or greater than MaxValue.
例
次のコード例では、メソッドを使用して、 Parse(String) 値の文字列形式を解析し Decimal ます。The following code example uses the Parse(String) method to parse string representations of Decimal values.
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'.
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
には、次の形式の数値が含まれています。Parameter s
contains a number of the form:
jax-wsシャープ[数字,] 数字 [. 小数部の桁] [ws][ws][sign][digits,]digits[.fractional-digits][ws]
角かっこ ([ および ]) 内の要素は省略可能です。Elements in square brackets ([ and ]) are optional. 次の表は、それぞれの要素の説明です。The following table describes each element.
要素Element | 説明Description |
---|---|
wsws | 省略可能な空白。Optional white space. |
signsign | 省略可能な符号。An optional sign. |
数値digits | 0から9までの一連の数字。A sequence of digits ranging from 0 to 9. |
、, | カルチャ固有の桁区切り記号。A culture-specific thousands separator symbol. |
.. | カルチャ固有の小数点の記号。A culture-specific decimal point symbol. |
小数点以下桁数fractional-digits | 0から9までの一連の数字。A sequence of digits ranging from 0 to 9. |
パラメーター s
はスタイルを使用して解釈され NumberStyles.Number ます。Parameter s
is interpreted using the NumberStyles.Number style. つまり、空白と桁区切り記号は許可されますが、通貨記号は使用できません。This means that white space and thousands separators are allowed but currency symbols are not. で使用できる要素 (通貨記号、桁区切り記号、空白文字など) を明示的に定義するに s
は、メソッドまたはメソッドを使用し Decimal.Parse(String, NumberStyles) Decimal.Parse(String, NumberStyles, IFormatProvider) ます。To explicitly define the elements (such as currency symbols, thousands separators, and white space) that can be present in s
, use either the Decimal.Parse(String, NumberStyles) or the Decimal.Parse(String, NumberStyles, IFormatProvider) method.
パラメーター s
は、 NumberFormatInfo 現在のシステムカルチャに対して初期化されたの書式情報を使用して解析されます。Parameter s
is parsed using the formatting information in a NumberFormatInfo initialized for the current system culture. 詳細については、「CurrentInfo」を参照してください。For more information, see CurrentInfo. 他のカルチャの書式設定情報を使用して文字列を解析するに Decimal.Parse(String, IFormatProvider) は、メソッドまたはメソッドを使用し Decimal.Parse(String, NumberStyles, IFormatProvider) ます。To parse a string using the formatting information of some other culture, use the Decimal.Parse(String, IFormatProvider) or Decimal.Parse(String, NumberStyles, IFormatProvider) method.
必要に応じて、の値は s
近似値に丸められて丸められます。If necessary, the value of s
is rounded using rounding to nearest.
の Decimal 有効桁数は29桁です。A Decimal has 29 digits of precision. が s
29 桁を超える数値を表していて、小数部を持ち、かつの範囲内にある場合、数値は切り捨てられず、切り捨てられずに、 MaxValue MinValue 近似値に丸められて29桁に丸められます。If s
represents a number that has more than 29 digits, but has a fractional part and is within the range of MaxValue and MinValue, the number is rounded, not truncated, to 29 digits using rounding to nearest.
解析操作中に、パラメーターで区切り記号が検出され、 s
該当する通貨または数値の10進数と桁区切り記号が同じである場合、解析操作では区切り記号が区切り記号ではなく小数点区切りであると見なされます。If during a parse operation a separator is encountered in the s
parameter, and the applicable currency or number decimal and group separators are the same, the parse operation assumes that the separator is a decimal separator rather than a group separator. 区切り記号の詳細については、「」、「」、「」、および「」を参照してください CurrencyDecimalSeparator NumberDecimalSeparator CurrencyGroupSeparator NumberGroupSeparator 。For more information about separators, see CurrencyDecimalSeparator, NumberDecimalSeparator, CurrencyGroupSeparator, and NumberGroupSeparator.