共用方式為


Single.Parse Method (String)

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Converts the string representation of a number to its single-precision floating-point number equivalent.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

Syntax

'Declaration
Public Shared Function Parse ( _
    s As String _
) As Single
public static float Parse(
    string s
)

Parameters

  • s
    Type: System.String
    A string representing a number to convert.

Return Value

Type: System.Single
A single-precision floating-point number equivalent to the numeric value or symbol specified in s.

Exceptions

Exception Condition
ArgumentNullException

s is nulla null reference (Nothing in Visual Basic).

FormatException

s is not a number in a valid format.

OverflowException

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

Remarks

The s parameter can contain PositiveInfinitySymbol, NegativeInfinitySymbol, NaNSymbol, or a string of the form:

[ws][sign] [integral-digits[,]]integral-digits[.[fractional-digits]][e[sign]exponential-digits][ws]

Elements in square brackets ([ and ]) are optional. The following table describes each element.

Element

Description

ws

A series of white space characters.

sign

A negative sign symbol or a positive sign symbol. Valid sign characters are determined by the NumberFormatInfo.NegativeSign and NumberFormatInfo.PositiveSign properties of the current culture. Only a leading sign can be used.

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 thousands separator symbol.

.

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.

The s parameter is interpreted using a combination of the NumberStyles.Float and NumberStyles.AllowThousands flags. This means that white space and thousands separators are allowed but currency symbols are not. To explicitly define the elements (such as currency symbols, thousands separators, and white space) that can be present in s, use the Parse(String, NumberStyles) method overload.

The s parameter is parsed by using the formatting information in a NumberFormatInfo object that is initialized for the current system culture. For more information, see CurrentInfo. To parse a string by using the formatting information of a specific culture, use the Parse(String, IFormatProvider) or Parse(String, NumberStyles, IFormatProvider) method.

Ordinarily, if you pass the Parse method a string that is created by calling the ToString method, the original Single value is returned. However, because of a loss of precision, the values may not be equal.

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.

Examples

The following example uses the Parse(String) method to convert an array of strings to equivalent Single values.

Module Example
   Public Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      Dim values() As String = {"100", "(100)", "-123,456,789", "123.45e+6", _
                                 "+500", "5e2", "3.1416", "600.", "-.123", _
                                 "-Infinity", "-1E-16", Double.MaxValue.ToString(), _
                                 Single.MinValue.ToString(), String.Empty}
      For Each value As String In values
         Try
            Dim number As Single = Single.Parse(value)
            outputBlock.Text += String.Format("{0} -> {1}", value, number) & vbCrLf
         Catch e As FormatException
            outputBlock.Text += String.Format("'{0}' is not in a valid format.", value) & vbCrLf
         Catch e As OverflowException
            outputBlock.Text += String.Format("{0} is outside the range of a Single.", value) & vbCrLf
         End Try
      Next
   End Sub
End Module
' The example displays the following output:
'       100 -> 100
'       '(100)' is not in a valid format.
'       -123,456,789 -> -1.234568E+08
'       123.45e+6 -> 1.2345E+08
'       +500 -> 500
'       5e2 -> 500
'       3.1416 -> 3.1416
'       600. -> 600
'       -.123 -> -0.123
'       -Infinity -> -Infinity
'       -1E-16 -> -1E-16
'       1.79769313486232E+308 is outside the range of a Single.
'       -3.402823E+38 -> -3.402823E+38
'       '' is not in a valid format.
using System;

public class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      string[] values = { "100", "(100)", "-123,456,789", "123.45e+6", 
                          "+500", "5e2", "3.1416", "600.", "-.123", 
                          "-Infinity", "-1E-16", Double.MaxValue.ToString(), 
                          Single.MinValue.ToString(), String.Empty };
      foreach (string value in values)
      {
         try
         {
            float number = Single.Parse(value);
            outputBlock.Text += String.Format("{0} -> {1}", value, number) + "\n";
         }
         catch (FormatException)
         {
            outputBlock.Text += String.Format("'{0}' is not in a valid format.", value) + "\n";
         }
         catch (OverflowException)
         {
            outputBlock.Text += String.Format("{0} is outside the range of a Single.", value) + "\n";
         }
      }
   }
}
// The example displays the following output:
//       100 -> 100
//       '(100)' is not in a valid format.
//       -123,456,789 -> -1.234568E+08
//       123.45e+6 -> 1.2345E+08
//       +500 -> 500
//       5e2 -> 500
//       3.1416 -> 3.1416
//       600. -> 600
//       -.123 -> -0.123
//       -Infinity -> -Infinity
//       -1E-16 -> -1E-16
//       1.79769313486232E+308 is outside the range of a Single.
//       -3.402823E+38 -> -3.402823E+38
//       '' is not in a valid format.

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.