Parse 메서드 (String, IFormatProvider)
이 문서는 기계로 번역한 것입니다. 원본 텍스트를 보려면 포인터를 문서의 문장 위로 올리십시오. 추가 정보
번역
원본

Single.Parse 메서드 (String, IFormatProvider)

 

지정된 문화권별 형식의 숫자에 대한 문자열 표현을 해당하는 단정밀도 부동 소수점 숫자로 변환합니다.

네임스페이스:   System
어셈블리:  mscorlib.dll의 mscorlib

public static float Parse(
	string s,
	IFormatProvider provider
)

매개 변수

s

변환할 숫자가 포함된 문자열입니다.

provider

s에 대한 문화권별 형식 지정 정보를 제공하는 개체입니다.

반환 값

Type: System.Single

s에 지정된 숫자 값 또는 기호에 해당하는 단정밀도 부동 소수점 숫자입니다.

Exception Condition
ArgumentNullException

s is null.

FormatException

s does not represent a number in a valid format.

OverflowException

s represents a number less than MinValue or greater than MaxValue.

This overload is typically used to convert text that can be formatted in a variety of ways to a Single value.For example, it can be used to convert the text entered by a user into an HTML text box to a numeric value.

The s parameter is interpreted using a combination of the NumberStyles.Float and NumberStyles.AllowThousands flags.The s parameter can contain NumberFormatInfo.PositiveInfinitySymbol, NumberFormatInfo.NegativeInfinitySymbol, or NumberFormatInfo.NaNSymbol for the culture specified by provider, or it can contain a string of the form:

[ws][sign]integral-digits[.[fractional-digits]][E[sign]exponential-digits][ws]

Optional elements are framed in square brackets ([ and ]).Elements that contain the term "digits" consist of a series of numeric characters ranging from 0 to 9.

Element

Description

ws

A series of white-space characters.

sign

A negative sign symbol (-) or a positive sign symbol (+).

integral-digits

A series of digits ranging from 0 to 9 that specify the integral part of the number.Runs of integral-digits can be partitioned by a group-separator symbol.For example, in some cultures a comma (,) separates groups of thousands.The integral-digits element can be absent if the string contains the fractional-digits element.

.

A culture-specific decimal point symbol.

fractional-digits

A series of digits ranging from 0 to 9 that specify the fractional part of the number.

E

The "e" or "E" character, which indicates that the value is represented in exponential (scientific) notation.

exponential-digits

A series of digits ranging from 0 to 9 that specify an exponent.

For more information about numeric formats, see the Formatieren von Typen in .NET Framework topic.

The provider parameter is an IFormatProvider implementation whose GetFormat method returns a NumberFormatInfo object that provides culture-specific formatting information.When the Parse(String, IFormatProvider) method is invoked, it calls the provider parameter's GetFormat method and passes it a Type object that represents the NumberFormatInfo type.The GetFormat method then returns the NumberFormatInfo object that provides information about the format of the s parameter.There are three ways to use the provider parameter to supply custom formatting information to the parse operation:

  • You can pass a CultureInfo object that represents the culture that supplies formatting information.Its GetFormat method returns the NumberFormatInfo object that provides numeric formatting information for that culture.

  • You can pass the actual NumberFormatInfo object that provides numeric formatting information.(Its implementation of GetFormat just returns itself.)

  • You can pass a custom object that implements IFormatProvider.Its GetFormat method instantiates and returns the NumberFormatInfo object that provides formatting information.

If provider is null or a NumberFormatInfo cannot be obtained, the formatting information for the current system culture is used.

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.For more information about separators, see CurrencyDecimalSeparator, NumberDecimalSeparator, CurrencyGroupSeparator, and NumberGroupSeparator.

Some examples of s are "100", "-123,456,789", "123.45e+6", "+500", "5e2", "3.1416", "600.", "-.123", and "-Infinity".

The following example is the button click event handler of a Web form.It uses the array returned by the HttpRequest.UserLanguages property to determine the user's locale.It then instantiates a CultureInfo object that corresponds to that locale.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 Single value.

protected void OkToSingle_Click(object sender, EventArgs e)
{
   string locale;
   float 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 = Single.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();
}

Universal Windows Platform
4.5 이후에 사용 가능
.NET Framework
1.1 이후에 사용 가능
Portable Class Library
portable .NET platforms에서 지원 portable .NET platforms
Silverlight
2.0 이후에 사용 가능
Windows Phone Silverlight
7.0 이후에 사용 가능
Windows Phone
8.1 이후에 사용 가능
맨 위로 이동
표시:
© 2016 Microsoft