Export (0) Print
Expand All

Single.Parse Method (String)

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

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

public static float Parse (
	string s
)
public static float Parse (
	String s
)
public static function Parse (
	s : String
) : float

Parameters

s

A string representing a number to convert.

Return Value

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

Exception typeCondition

ArgumentNullException

s is a 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.

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

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

Optional items are framed in square brackets ([ and ]). Items containing the term "digits" consist of a series of numeric characters ranging from 0 to 9.

ws

A series of white space characters.

sign

A negative sign or positive sign symbol.

integral-digits

A series of digits specifying 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.) Integral-digits can be absent if there are fractional-digits.

'.'

A culture-specific decimal point symbol.

fractional-digits

A series of digits specifying the fractional part of the number.

'e'

An uppercase or lowercase character 'e', indicating exponential (scientific) notation.

exponential-digits

A series of digits specifying an exponent.

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

This version of Parse uses the NumberStyles values, Float and AllowThousands, and the culture-specific NumberFormatInfo data associated with the current thread.

For more information about numeric formats, see the Formatting Overview topic.

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.

The following code example demonstrates the Parse method.

bool done = false;
string inp;
do
{
    Console.Write("Enter a real number: ");
    inp = Console.ReadLine();
    try
    {
        s = Single.Parse(inp);
        Console.WriteLine("You entered {0}.", s.ToString());
        done = true;
    }
    catch (FormatException)
    {
        Console.WriteLine("You did not enter a number.");
    }
    catch (Exception e)
    {
        Console.WriteLine("An exception occurred while parsing your response: {0}", e.ToString());
    }
} while (!done);

boolean done = false;
System.String inp;
do {
    Console.Write("Enter a real number: ");
    inp = Console.ReadLine();
    try {
        s = new Float(Float.parseFloat(inp));
        Console.WriteLine("You entered {0}.", s);
        done = true;
    }
    catch (FormatException exp) {
        Console.WriteLine("You did not enter a number.");
    }
    catch (System.Exception e) {
        Console.WriteLine("An exception occurred while parsing your"
            + " response: {0}", e.ToString());
    }
} while (!(done));

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft