Decimal.Parse 메서드
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
숫자의 문자열 표현을 해당하는 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)
- Source:
- Decimal.cs
- Source:
- Decimal.cs
- Source:
- 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
에 포함된 숫자에 해당합니다.
예외
s
이(가) null
인 경우
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] [기호] [digits,]digits[.fractional-digits][ws]
대괄호 ([ 및 ]) 안의 요소는 선택적 요소입니다. 다음 표에서는 각 요소에 대해 설명합니다.
요소 | 설명 |
---|---|
ws | 선택적 공백입니다. |
sign | 선택적 기호입니다. |
숫자 | 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, NumberStyles, IFormatProvider) 메서드를 Decimal.Parse(String, IFormatProvider) 사용합니다.
필요한 경우 의 값 s
은 반올림을 사용하여 가장 가까운 값으로 반올림됩니다.
Decimal 에는 29자리의 정밀도가 있습니다. 29자리를 초과하지만 소수 부분이 있고 및 MinValue범위 MaxValue 내에 있는 숫자를 나타내는 경우 s
반올림을 사용하여 가장 가까운 숫자로 반올림되고 잘리지 않고 29자리로 반올림됩니다.
구문 분석 작업 중에 매개 변수에서 s
구분 기호가 발견되고 해당 통화 또는 숫자 10진수 및 그룹 구분 기호가 동일한 경우 구문 분석 작업은 구분 기호가 그룹 구분 기호가 아닌 소수 구분 기호라고 가정합니다. 구분 기호에 대한 자세한 내용은 , , NumberDecimalSeparatorCurrencyGroupSeparator및 NumberGroupSeparator를 참조CurrencyDecimalSeparator하세요.
추가 정보
적용 대상
Parse(ReadOnlySpan<Byte>, IFormatProvider)
- Source:
- Decimal.cs
- Source:
- 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)
- Source:
- Decimal.cs
- Source:
- Decimal.cs
- Source:
- 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)
- Source:
- Decimal.cs
- Source:
- Decimal.cs
- Source:
- 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 숫자입니다.
예외
s
이(가) null
인 경우
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] [$] [기호] [digits,]digits[.fractional-digits][e[sign]digits][ws]
대괄호 ([ 및 ]) 안의 요소는 선택적 요소입니다. 다음 표에서는 각 요소에 대해 설명합니다.
요소 | 설명 |
---|---|
ws | 선택적 공백입니다. 플래그를 포함하는 경우 style 의 시작 부분에 s 공백이 NumberStyles.AllowLeadingWhite 표시될 수 있으며 플래그가 포함된 NumberStyles.AllowTrailingWhite 경우 style 의 s 끝에 표시될 수 있습니다. |
$ | 문화권별 통화 기호입니다. 문자열의 위치는 현재 문화권의 NumberFormatInfo.CurrencyNegativePattern 및 NumberFormatInfo.CurrencyPositivePattern 속성에 의해 정의됩니다. 플래그가 포함된 경우 style 현재 문화권의 통화 기호가 NumberStyles.AllowCurrencySymbol 에 s 나타날 수 있습니다. |
sign | 선택적 기호입니다. 플래그가 포함된 경우 style 의 시작 부분에 s 기호가 NumberStyles.AllowLeadingSign 표시되고 플래그가 포함된 NumberStyles.AllowTrailingSign 경우 style 의 s 끝에 표시할 수 있습니다. 플래그가 포함된 NumberStyles.AllowParentheses 경우 style 괄호를 사용하여 s 음수 값을 나타낼 수 있습니다. |
숫자 | 0에서 9 사이의 숫자 시퀀스입니다. |
, | 문화권별 천 단위 구분 기호입니다. 플래그가 포함된 경우 style 현재 문화권의 천 단위 구분 기호가 NumberStyles.AllowThousands 에 s 나타날 수 있습니다. |
. | 문화권별 소수점 기호입니다. 플래그가 포함된 경우 style 현재 문화권의 소수점 기호가 NumberStyles.AllowDecimalPoint 에 s 나타날 수 있습니다. |
fractional-digits | 0에서 9 사이의 숫자 시퀀스입니다. 소수 자릿수는 플래그가 포함된 경우에만 style 에 s 표시할 NumberStyles.AllowDecimalPoint 수 있습니다. |
e | 값이 지수 표기법으로 표시됨을 나타내는 'e' 또는 'E' 문자입니다. 매개 변수는 s 플래그를 포함하는 경우 style 지수 표기법으로 NumberStyles.AllowExponent 숫자를 나타낼 수 있습니다. |
참고
의 종결 NUL(U+0000) 문자 s
는 인수 값 style
에 관계없이 구문 분석 작업에서 무시됩니다.
숫자만 있는 문자열(스타일에 None 해당)은 형식 범위에 Decimal 있는 경우 항상 성공적으로 구문 분석됩니다. 나머지 NumberStyles 멤버는 입력 문자열에 있을 수 있지만 필요하지 않은 요소를 제어합니다. 다음 표에서는 개별 NumberStyles 멤버가 에 s
있을 수 있는 요소에 미치는 영향을 나타냅니다.
NumberStyles 값 | 숫자 외에 에서 허용되는 요소 |
---|---|
None | digits 요소만 해당합니다. |
AllowDecimalPoint | 및소수 자릿수 요소입니다. |
AllowExponent | 매개 변수는 s 지수 표기법을 사용할 수도 있습니다. 이 플래그는 양식 숫자 E 숫자의 값을 지원합니다. 양수 또는 음수 기호 및 소수점 기호와 같은 요소를 사용하여 문자열을 성공적으로 구문 분석하려면 추가 플래그가 필요합니다. |
AllowLeadingWhite | 의 시작 부분에 있는 ws 요소입니다 s . |
AllowTrailingWhite | 의 끝에 있는 ws 요소입니다 s . |
AllowLeadingSign | 의 시작 부분에 있는 sign 요소입니다 s . |
AllowTrailingSign | 의 끝에 있는 sign 요소입니다 s . |
AllowParentheses | 숫자 값을 묶는 괄호 형식의 sign 요소입니다. |
AllowThousands | , 요소입니다. |
AllowCurrencySymbol | $ 요소입니다. |
Currency | 모두. 매개 변수는 s 16진수 또는 지수 표기법의 숫자를 나타낼 수 없습니다. |
Float | 의 시작 또는 끝에 s 있는 ws 요소 및 기호의 s 시작 부분에 서명합니다. . 매개 변수는 s 지수 표기법을 사용할 수도 있습니다. |
Number |
ws , sign 및 , . 요소입니다. |
Any | 를 제외한 s 모든 스타일은 16진수를 나타낼 수 없습니다. |
s
매개 변수는 현재 시스템 문화권에 대해 초기화된 개체의 NumberFormatInfo 서식 정보를 사용하여 구문 분석됩니다. 자세한 내용은 CurrentInfo를 참조하세요.
Decimal 에는 29자리의 정밀도가 있습니다. 숫자가 29자리를 초과하지만 소수 부분이 있고 및 MinValue범위 MaxValue 내에 있는 숫자를 나타내는 경우 s
가장 가까운 로 반올림을 사용하여 숫자가 반올림되지 않고 29자리로 반올림됩니다.
구문 분석 작업 styles
중에 매개 변수에서 s
구분 기호가 발견되면 및 값이 NumberStyles.AllowDecimalPoint 포함 NumberStyles.AllowThousands 되고 해당 통화 또는 숫자 10진수 및 그룹 구분 기호가 동일한 경우 구문 분석 작업은 구분 기호가 그룹 구분 기호가 아닌 10진수 구분 기호라고 가정합니다. 구분 기호에 대한 자세한 내용은 , , NumberDecimalSeparatorCurrencyGroupSeparator및 NumberGroupSeparator를 참조CurrencyDecimalSeparator하세요.
추가 정보
적용 대상
Parse(String, IFormatProvider)
- Source:
- Decimal.cs
- Source:
- Decimal.cs
- Source:
- 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 숫자입니다.
구현
예외
s
이(가) null
인 경우
s
의 형식이 올바르지 않습니다.
s
는 Decimal.MinValue 보다 작거나 Decimal.MaxValue보다 큰 숫자를 나타냅니다.
예제
다음 예제는 웹 양식의 단추 클릭 이벤트 처리기입니다. 속성에서 반환된 배열을 HttpRequest.UserLanguages 사용하여 사용자의 로캘을 확인합니다. 그런 다음 해당 로캘에 CultureInfo 해당하는 개체를 인스턴스화합니다. NumberFormatInfo 그런 다음 해당 CultureInfo 개체에 속하는 개체가 메서드에 Parse(String, IFormatProvider) 전달되어 사용자의 입력을 값으로 Decimal 변환합니다.
protected void OkToDecimal_Click(object sender, EventArgs e)
{
string locale;
decimal number;
CultureInfo culture;
// Return if string is empty
if (String.IsNullOrEmpty(this.inputNumber.Text))
return;
// Get locale of web request to determine possible format of number
if (Request.UserLanguages.Length == 0)
return;
locale = Request.UserLanguages[0];
if (String.IsNullOrEmpty(locale))
return;
// Instantiate CultureInfo object for the user's locale
culture = new CultureInfo(locale);
// Convert user input from a string to a number
try
{
number = Decimal.Parse(this.inputNumber.Text, culture.NumberFormat);
}
catch (FormatException)
{
return;
}
catch (Exception)
{
return;
}
// Output number to label on web form
this.outputNumber.Text = "Number is " + number.ToString();
}
Protected Sub OkToDecimal_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles OkToDecimal.Click
Dim locale As String
Dim culture As CultureInfo
Dim number As Decimal
' Return if string is empty
If String.IsNullOrEmpty(Me.inputNumber.Text) Then Exit Sub
' Get locale of web request to determine possible format of number
If Request.UserLanguages.Length = 0 Then Exit Sub
locale = Request.UserLanguages(0)
If String.IsNullOrEmpty(locale) Then Exit Sub
' Instantiate CultureInfo object for the user's locale
culture = New CultureInfo(locale)
' Convert user input from a string to a number
Try
number = Decimal.Parse(Me.inputNumber.Text, culture.NumberFormat)
Catch ex As FormatException
Exit Sub
Catch ex As Exception
Exit Sub
End Try
' Output number to label on web form
Me.outputNumber.Text = "Number is " & number.ToString()
End Sub
설명
메서드의 Parse(String, IFormatProvider) 이 오버로드는 다양한 방법으로 서식을 지정할 수 있는 텍스트를 값으로 변환하는 Decimal 데 일반적으로 사용됩니다. 예를 들어 사용자가 입력한 텍스트를 HTML 텍스트 상자로 숫자 값으로 변환하는 데 사용할 수 있습니다.
매개 변수에는 s
다음과 같은 여러 양식이 포함됩니다.
[ws] [sign] [digits,]digits[.fractional-digits][ws]
대괄호 ([ 및 ]) 안의 요소는 선택적 요소입니다. 다음 표에서는 각 요소에 대해 설명합니다.
요소 | 설명 |
---|---|
ws | 선택적 공백입니다. |
sign | 선택적 기호입니다. |
숫자 | 0에서 9까지의 숫자 시퀀스입니다. |
, | 문화권별 천 단위 구분 기호입니다. |
. | 문화권별 소수점 기호입니다. |
fractional-digits | 0에서 9까지의 숫자 시퀀스입니다. |
매개 변수는 s
스타일을 사용하여 해석됩니다 NumberStyles.Number . 즉, 공백과 수천 개의 구분 기호가 허용되지만 통화 기호는 허용되지 않습니다. 에 있을 s
수 있는 요소(예: 통화 기호, 천 구분 기호 및 공백)를 명시적으로 정의하려면 메서드를 Decimal.Parse(String, NumberStyles, IFormatProvider) 사용합니다.
provider
매개 변수는 또는 CultureInfo 개체와 같은 구현입니다 NumberFormatInfoIFormatProvider. 매개 변수는 provider
구문 분석에 사용되는 문화권별 정보를 제공합니다.
provider
가 null
이면 현재 스레드 문화권이 사용됩니다.
개체의 Decimal 전체 자릿수는 29자리입니다. 숫자가 29자리를 초과하지만 소수 부분이 있고 및 MinValue범위 MaxValue 내에 있는 숫자를 나타내는 경우 s
가장 가까운 로 반올림을 사용하여 숫자가 반올림되지 않고 29자리로 반올림됩니다.
구문 분석 작업 중에 매개 변수에서 s
구분 기호가 발견되고 해당 통화 또는 숫자 10진수 및 그룹 구분 기호가 동일한 경우 구문 분석 작업은 구분 기호가 그룹 구분 기호가 아닌 10진수 구분 기호라고 가정합니다. 구분 기호에 대한 자세한 내용은 , , NumberDecimalSeparatorCurrencyGroupSeparator및 NumberGroupSeparator를 참조CurrencyDecimalSeparator하세요.
추가 정보
적용 대상
Parse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider)
- Source:
- Decimal.cs
- Source:
- 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)
- Source:
- Decimal.cs
- Source:
- Decimal.cs
- Source:
- 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)
- Source:
- Decimal.cs
- Source:
- Decimal.cs
- Source:
- 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보다 큰 숫자를 나타냅니다.
s
은 null
입니다.
예제
다음 예제에서는 다양한 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] [$] [기호] [digits,]digits[.fractional-digits][e[sign]digits][ws]
대괄호 ([ 및 ]) 안의 요소는 선택적 요소입니다. 다음 표에서는 각 요소에 대해 설명합니다.
요소 | 설명 |
---|---|
$ | 문화권별 통화 기호입니다. 문자열의 해당 위치는 매개 변수의 메서드 provider 에서 반환된 NumberFormatInfo 개체의 및 CurrencyPositivePattern 속성에 GetFormat 의해 CurrencyNegativePattern 정의됩니다. 플래그가 포함된 경우 style 통화 기호가 NumberStyles.AllowCurrencySymbol 에 s 나타날 수 있습니다. |
ws | 선택적 공백입니다. 플래그를 포함하는 경우 style 의 시작 부분에 s 공백이 NumberStyles.AllowLeadingWhite 표시될 수 있으며 플래그가 포함된 NumberStyles.AllowTrailingWhite 경우 style 의 s 끝에 표시될 수 있습니다. |
sign | 선택적 기호입니다. 플래그가 포함된 경우 style 의 시작 부분에 s 기호가 NumberStyles.AllowLeadingSign 표시되고 플래그가 포함된 NumberStyles.AllowTrailingSign 경우 style 의 s 끝에 표시할 수 있습니다. 플래그가 포함된 NumberStyles.AllowParentheses 경우 style 괄호를 사용하여 s 음수 값을 나타낼 수 있습니다. |
숫자 | 0에서 9 사이의 숫자 시퀀스입니다. |
, | 문화권별 천 단위 구분 기호입니다. 에 정의된 provider 문화권의 천 단위 구분 기호가 플래그를 포함하는 NumberStyles.AllowThousands 경우 style 에 s 나타날 수 있습니다. |
. | 문화권별 소수점 기호입니다. 에 정의된 provider 문화권의 소수점 기호는 플래그가 포함된 NumberStyles.AllowDecimalPoint 경우 style 에 s 나타날 수 있습니다. |
fractional-digits | 0에서 9 사이의 숫자 시퀀스입니다. 소수 자릿수는 플래그가 포함된 경우에만 style 에 s 표시할 NumberStyles.AllowDecimalPoint 수 있습니다. |
e | 값이 지수 표기법으로 표시됨을 나타내는 'e' 또는 'E' 문자입니다. 플래그가 포함된 경우 style 매개 변수는 s 지수 표기법으로 NumberStyles.AllowExponent 숫자를 나타낼 수 있습니다. |
참고
의 종결 NUL(U+0000) 문자는 인수 값 style
에 관계없이 구문 분석 작업에서 s
무시됩니다.
숫자만 있는 문자열(스타일에 None 해당)은 형식의 Decimal 범위에 있는 경우 항상 성공적으로 구문 분석됩니다. 나머지 NumberStyles 멤버는 입력 문자열에 있을 수 있지만 필요하지 않은 요소를 제어합니다. 다음 표에서는 개별 NumberStyles 멤버가 에 s
있을 수 있는 요소에 미치는 영향을 나타냅니다.
NumberStyles 값 | 숫자 외에 에서 허용되는 요소 |
---|---|
None | digits 요소만 해당합니다. |
AllowDecimalPoint | 및소수 자릿수 요소입니다. |
AllowExponent | 매개 변수는 s 지수 표기법을 사용할 수도 있습니다. 이 플래그는 양식 숫자 E 숫자의 값을 지원합니다. 양수 또는 음수 기호 및 소수점 기호와 같은 요소로 문자열을 구문 분석하려면 추가 플래그가 필요합니다. |
AllowLeadingWhite | 의 시작 부분에 있는 ws 요소입니다 s . |
AllowTrailingWhite | 의 끝에 있는 ws 요소입니다 s . |
AllowLeadingSign | 의 시작 부분에 있는 기호 요소입니다 s . |
AllowTrailingSign | 의 끝에 있는 기호 요소입니다 s . |
AllowParentheses | 숫자 값을 묶는 괄호 형식의 기호 요소입니다. |
AllowThousands | , 요소입니다. |
AllowCurrencySymbol | $ 요소입니다. |
Currency | 모두. 매개 변수는 s 16진수 또는 지수 표기법으로 숫자를 나타낼 수 없습니다. |
Float | 의 s 시작 또는 끝에 있는 ws 요소, 의 시작 s 부분에 기호, 및 기호입니다. 매개 변수는 s 지수 표기법을 사용할 수도 있습니다. |
Number | , , , 및 . 요소입니다. sign ws |
Any | 를 제외한 s 모든 스타일은 16진수를 나타낼 수 없습니다. |
매개 변수는 provider
또는 개체와 같은 구현입니다 NumberFormatInfoIFormatProviderCultureInfo. 매개 변수는 provider
구문 분석에 사용되는 문화권별 정보를 제공합니다.
provider
가 null
이면 현재 스레드 문화권이 사용됩니다.
개체의 Decimal 전체 자릿수는 29자리입니다. 29자리를 초과하지만 소수 부분이 있고 및 MinValue범위 MaxValue 내에 있는 숫자를 나타내는 경우 s
반올림을 사용하여 가장 가까운 숫자로 반올림되고 잘리지 않고 29자리로 반올림됩니다.
구문 분석 작업 중에 매개 변수에서 s
구분 기호가 발견되고 해당 통화 또는 숫자 10진수 및 그룹 구분 기호가 동일한 경우 구문 분석 작업은 구분 기호가 그룹 구분 기호가 아닌 소수 구분 기호라고 가정합니다. 구분 기호에 대한 자세한 내용은 , , NumberDecimalSeparatorCurrencyGroupSeparator및 NumberGroupSeparator를 참조CurrencyDecimalSeparator하세요.
추가 정보
적용 대상
.NET
피드백
https://aka.ms/ContentUserFeedback
출시 예정: 2024년 내내 콘텐츠에 대한 피드백 메커니즘으로 GitHub 문제를 단계적으로 폐지하고 이를 새로운 피드백 시스템으로 바꿀 예정입니다. 자세한 내용은 다음을 참조하세요.다음에 대한 사용자 의견 제출 및 보기