Skip to main content
.NET Framework Class Library
SingleToString Method

Converts the numeric value of this instance to its equivalent string representation.

Namespace:   System
Assembly:  mscorlib (in mscorlib.dll)
Syntax
Public Overrides Function ToString As [%$TOPIC/w5dbkt9x_en-us_VS_110_1_0_0_0_0%]
public override [%$TOPIC/w5dbkt9x_en-us_VS_110_1_0_1_0_0%] ToString()
public:
virtual [%$TOPIC/w5dbkt9x_en-us_VS_110_1_0_2_0_0%]^ ToString() override
abstract ToString : unit -> [%$TOPIC/w5dbkt9x_en-us_VS_110_1_0_3_0_0%]  
override ToString : unit -> [%$TOPIC/w5dbkt9x_en-us_VS_110_1_0_3_0_1%]

Return Value

Type: SystemString
The string representation of the value of this instance.
Remarks

The ToString method formats a Single value in the default ("G", or general) format of the current culture. If you want to specify a different format or culture, use the other overloads of the ToString method, as follows:

To use format

For culture

Use the overload

Default ("G") format

A specific culture

ToString(IFormatProvider)

A specific format

Default (current) culture

ToString(String)

A specific format

A specific culture

ToString(String, IFormatProvider)

The return value can be PositiveInfinitySymbol, NegativeInfinitySymbol, NaNSymbol, or a string of the form:

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

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. The following table lists each element:

Element

Description

sign

A negative sign or positive sign symbol.

integral-digits

A series of digits specifying the integral part of the number. 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'

A lowercase character 'e', indicating exponential (scientific) notation.

exponential-digits

A series of digits specifying an exponent.

Some examples of the return value are "100", "-123,456,789", "123.45e+6", "500", "3.1416", "600", "-0.123", and "-Infinity".

The .NET Framework provides extensive formatting support, which is described in greater detail in the following formatting topics:

Examples

The following example uses the default SingleToString method to display the string representations of a number of Single values.

Dim number As Single

number = 1.6E20
' Displays 1.6E+20.      
Console.WriteLine(number.ToString())

number = 1.6E2
' Displays 160.
Console.WriteLine(number.ToString())

number = -3.541
' Displays -3.541.
Console.WriteLine(number.ToString())

number = -1502345222199E-07
' Displays -150234.5222199.
Console.WriteLine(number.ToString())

number = -15023452221990199574E-09
' Displays -15023452221.9902.
Console.WriteLine(number.ToString())

number = .60344
' Displays 0.60344.
Console.WriteLine(number.ToString())

number = .000000001
' Displays 1E-09.
Console.WriteLine(number.ToString())
float number;

number = 1.6E20F;
// Displays 1.6E+20.
Console.WriteLine(number.ToString());

number = 1.6E2F;
// Displays 160.
Console.WriteLine(number.ToString());

number = -3.541F;
// Displays -3.541.
Console.WriteLine(number.ToString());

number = -1502345222199E-07F;
// Displays -150234.5222199.
Console.WriteLine(number.ToString());

number = -15023452221990199574E-09F;
// Displays -15023452221.9902.
Console.WriteLine(number.ToString());

number = .60344F;
// Displays 0.60344.
Console.WriteLine(number.ToString());

number = .000000001F;
// Displays 1E-09.
Console.WriteLine(number.ToString());

The following code example illustrates the use of the Parse(String) method along with the ToString method.

Dim Done As Boolean = False 
Dim Inp As String 
Do

    Console.Write("Enter a real number: ")
    Inp = Console.ReadLine()
    Try
        S = Single.Parse(Inp)
        Console.WriteLine("You entered " + S.ToString() + ".")
        Done = True 
    Catch E As FormatException
        Console.WriteLine("You did not enter a number.")
    Catch E As Exception
        Console.WriteLine("An exception occurred while parsing your response: " + E.ToString())
    End Try 
Loop While Not Done
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);
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 );
      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 );
   }
}
while (  !done );
Version Information

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone 8, Silverlight 8.1
Platforms

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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