Export (0) Print
Expand All

Single.ToString Method

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

Overload List

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

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Overrides Public Function ToString() As String
[C#] public override string ToString();
[C++] public: String* ToString();
[JScript] public override function ToString() : String;

Converts the numeric value of this instance to its equivalent string representation using the specified culture-specific format information.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Overridable Function ToString(IFormatProvider) As String
[C#] public virtual string ToString(IFormatProvider);
[C++] public: virtual String* ToString(IFormatProvider*);
[JScript] public function ToString(IFormatProvider) : String;

Converts the numeric value of this instance to its equivalent string representation, using the specified format.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Function ToString(String) As String
[C#] public string ToString(string);
[C++] public: String* ToString(String*);
[JScript] public function ToString(String) : String;

Converts the numeric value of this instance to its equivalent string representation using the specified format and culture-specific format information.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Overridable Function ToString(String, IFormatProvider) As String
[C#] public virtual string ToString(string, IFormatProvider);
[C++] public: virtual String* ToString(String*, IFormatProvider*);
[JScript] public function ToString(String, IFormatProvider) : String;

Example

[Visual Basic, C#, C++] The following code example converts a Single value to a String with the ToString method, using a formatting String and an IFormatProvider object.

[Visual Basic, C#, C++] Note   This example shows how to use one of the overloaded versions of ToString. For other examples that might be available, see the individual overload topics.
[Visual Basic] 
' Example for the Single.ToString( ) methods.
Imports System
Imports System.Globalization
Imports Microsoft.VisualBasic

Module SingleToStringDemo
    
    Sub Main( )

        Dim singleValue As Single = 11876.54321
        Dim format      As String = "  {0,-30}{1}"
            
        Console.WriteLine( "This example of" & vbCrLf & _
            "  Single.ToString( ), " & vbCrLf & _
            "  Single.ToString( String )," & vbCrLf & _
            "  Single.ToString( IFormatProvider ), and " & vbCrLf & _ 
            "  Single.ToString( String, IFormatProvider )" & _
            vbCrLf & "generates the following output when run in " & _
            "the [{0}] culture. " & vbCrLf & "A Single number is " & _
            "formatted with various combinations of format " & _
            "" & vbCrLf & "strings and IFormatProvider.", _
            CultureInfo.CurrentCulture.Name )

        ' Format the number without and with format strings.
        Console.WriteLine( vbCrLf & "IFormatProvider is not " & _
            "used; the default culture is [{0}]:", _
            CultureInfo.CurrentCulture.Name )
        Console.WriteLine( format, _
            "No format string:", singleValue.ToString( ) )
        Console.WriteLine( format, _
            "'N5' format string:", singleValue.ToString( "N5" ) )
        Console.WriteLine( format, _
            "'E' format string:", singleValue.ToString( "E" ) )
        Console.WriteLine( format, _
            "'E5' format string:", singleValue.ToString( "E5" ) )
            
        ' Create a CultureInfo object for another culture. Use
        ' [Dutch - The Netherlands] unless the current culture
        ' is Dutch language. In that case use [English - U.S.].
        Dim cultureName As String = IIf( _
            CultureInfo.CurrentCulture.Name.Substring( 0, 2 ) = _
                "nl", "en-US", "nl-NL" )
        Dim culture As New CultureInfo( cultureName )
            
        ' Use the CultureInfo object for an IFormatProvider.
        Console.WriteLine( vbCrLf & "A CultureInfo object " & _
            "for [{0}] is used for the IFormatProvider: ", _
            cultureName )
        Console.WriteLine( format, "No format string:", _
            singleValue.ToString( culture ) )
        Console.WriteLine( format, "'N5' format string:", _
            singleValue.ToString( "N5", culture ) )
        Console.WriteLine( format, "'E' format string:", _
            singleValue.ToString( "E", culture ) )
            
        ' Get the NumberFormatInfo object from CultureInfo, and
        ' then change the digit group size to 2 and the digit
        ' separator to '_'.
        Dim numInfo As NumberFormatInfo = culture.NumberFormat
        numInfo.NumberGroupSizes = New Integer( ) { 2 }
        numInfo.NumberGroupSeparator = "_"
            
        ' Use a NumberFormatInfo object for IFormatProvider.
        Console.WriteLine( vbCrLf & _
            "A NumberFormatInfo object with digit group " & _
            "size = 2 and " & vbCrLf & "digit separator " & _
            "= '_' is used for the IFormatProvider:" )
        Console.WriteLine( format, "'N' format string:", _
            singleValue.ToString( "N", culture ) )
        Console.WriteLine( format, "'E' format string:", _
            singleValue.ToString( "E", culture ) )
    End Sub 
End Module 

' This example of
'   Single.ToString( ),
'   Single.ToString( String ),
'   Single.ToString( IFormatProvider ), and
'   Single.ToString( String, IFormatProvider )
' generates the following output when run in the [en-US] culture.
' A Single number is formatted with various combinations of format
' strings and IFormatProvider.
' 
' IFormatProvider is not used; the default culture is [en-US]:
'   No format string:             11876.54
'   'N5' format string:           11,876.54000
'   'E' format string:            1.187654E+004
'   'E5' format string:           1.18765E+004
' 
' A CultureInfo object for [nl-NL] is used for the IFormatProvider:
'   No format string:             11876,54
'   'N5' format string:           11.876,54000
'   'E' format string:            1,187654E+004
' 
' A NumberFormatInfo object with digit group size = 2 and
' digit separator = '_' is used for the IFormatProvider:
'   'N' format string:            1_18_76,54
'   'E' format string:            1,187654E+004

[C#] 
// Example for the Single.ToString( ) methods.
using System;
using System.Globalization;

class SingleToStringDemo
{
    public static void Main( )
    {
        float  singleValue = 11876.54321F;
        string format      = "  {0,-30}{1}";
            
        Console.WriteLine( "This example of\n" +
            "  Single.ToString( ), \n" +
            "  Single.ToString( String ),\n" +
            "  Single.ToString( IFormatProvider ), and \n" +
            "  Single.ToString( String, IFormatProvider )\n" +
            "generates the following output when run in the [{0}] " +
            "culture. \nA Single number is formatted with various " +
            "combinations of format \nstrings and IFormatProvider.", 
            CultureInfo.CurrentCulture.Name );

        // Format the number without and with format strings.
        Console.WriteLine( "\nIFormatProvider is not " +
            "used; the default culture is [{0}]:", 
            CultureInfo.CurrentCulture.Name );
        Console.WriteLine( format, "No format string:", 
            singleValue.ToString( ) );
        Console.WriteLine( format, "'N5' format string:", 
            singleValue.ToString( "N5" ) );
        Console.WriteLine( format, "'E' format string:", 
            singleValue.ToString( "E" ) );
        Console.WriteLine( format, "'E5' format string:", 
            singleValue.ToString( "E5" ) );
            
        // Create a CultureInfo object for another culture. Use
        // [Dutch - The Netherlands] unless the current culture
        // is Dutch language. In that case use [English - U.S.].
        string cultureName = 
            CultureInfo.CurrentCulture.Name.Substring( 0, 2 ) == "nl" ? 
                "en-US" : "nl-NL";
        CultureInfo culture = new CultureInfo( cultureName );
            
        // Use the CultureInfo object for an IFormatProvider.
        Console.WriteLine( "\nA CultureInfo object " +
            "for [{0}] is used for the IFormatProvider: ", 
            cultureName );
        Console.WriteLine( format, "No format string:", 
            singleValue.ToString( culture ) );
        Console.WriteLine( format, "'N5' format string:", 
            singleValue.ToString( "N5", culture ) );
        Console.WriteLine( format, "'E' format string:", 
            singleValue.ToString( "E", culture ) );
            
        // Get the NumberFormatInfo object from CultureInfo, and
        // then change the digit group size to 2 and the digit
        // separator to '_'.
        NumberFormatInfo numInfo = culture.NumberFormat;
        numInfo.NumberGroupSizes = new int[ ]{ 2 };
        numInfo.NumberGroupSeparator = "_";
            
        // Use a NumberFormatInfo object for IFormatProvider.
        Console.WriteLine( "\nA NumberFormatInfo object with digit " +
            "group size = 2 and \ndigit separator = '_' is used " +
            "for the IFormatProvider:" );
        Console.WriteLine( format, "'N' format string:", 
            singleValue.ToString( "N", culture ) );
        Console.WriteLine( format, "'E' format string:", 
            singleValue.ToString( "E", culture ) );
    }
}

/*
This example of
  Single.ToString( ),
  Single.ToString( String ),
  Single.ToString( IFormatProvider ), and
  Single.ToString( String, IFormatProvider )
generates the following output when run in the [en-US] culture.
A Single number is formatted with various combinations of format
strings and IFormatProvider.

IFormatProvider is not used; the default culture is [en-US]:
  No format string:             11876.54
  'N5' format string:           11,876.54000
  'E' format string:            1.187654E+004
  'E5' format string:           1.18765E+004

A CultureInfo object for [nl-NL] is used for the IFormatProvider:
  No format string:             11876,54
  'N5' format string:           11.876,54000
  'E' format string:            1,187654E+004

A NumberFormatInfo object with digit group size = 2 and
digit separator = '_' is used for the IFormatProvider:
  'N' format string:            1_18_76,54
  'E' format string:            1,187654E+004
*/ 

[C++] 
// Example for the Single::ToString( ) methods.
#using <mscorlib.dll>
using namespace System;
using namespace System::Globalization;

void main( )
{
    float   singleValue = 11876.54321F;
    String* format      = S"  {0,-30}{1}";
        
    Console::WriteLine( S"This example of\n" 
        S"  Single::ToString( ), \n" 
        S"  Single::ToString( String* ),\n" 
        S"  Single::ToString( IFormatProvider* ), and \n" 
        S"  Single::ToString( String*, IFormatProvider* )\n" 
        S"generates the following output when run in the [{0}] " 
        S"culture. \nA Single number is formatted with various " 
        S"combinations of format \nstrings and IFormatProvider.", 
        CultureInfo::CurrentCulture->Name );

    // Format the number without and with format strings.
    Console::WriteLine( S"\nIFormatProvider is not " 
        S"used; the default culture is [{0}]:", 
        CultureInfo::CurrentCulture->Name );
    Console::WriteLine( format, S"No format string:", 
        singleValue.ToString( ) );
    Console::WriteLine( format, S"'N5' format string:", 
        singleValue.ToString( S"N5" ) );
    Console::WriteLine( format, S"'E' format string:", 
        singleValue.ToString( S"E" ) );
    Console::WriteLine( format, S"'E5' format string:", 
        singleValue.ToString( S"E5" ) );
        
    // Create a CultureInfo object for another culture. Use
    // [Dutch - The Netherlands] unless the current culture
    // is Dutch language. In that case use [English - U.S.].
    String* cultureName = 
        CultureInfo::CurrentCulture->Name->Substring( 0, 2 ) == S"nl" ? 
            S"en-US" : S"nl-NL";
    CultureInfo* culture = new CultureInfo( cultureName );
        
    // Use the CultureInfo object for an IFormatProvider.
    Console::WriteLine( S"\nA CultureInfo object " 
        S"for [{0}] is used for the IFormatProvider: ", 
        cultureName );
    Console::WriteLine( format, S"No format string:", 
        singleValue.ToString( culture ) );
    Console::WriteLine( format, S"'N5' format string:", 
        singleValue.ToString( S"N5", culture ) );
    Console::WriteLine( format, S"'E' format string:", 
        singleValue.ToString( S"E", culture ) );
        
    // Get the NumberFormatInfo object from CultureInfo, and
    // then change the digit group size to 2 and the digit
    // separator to '_'.
    NumberFormatInfo* numInfo = culture->NumberFormat;
    Int32 sizes __gc [] = { 2 };
    numInfo->NumberGroupSizes = sizes;
    numInfo->NumberGroupSeparator = S"_";
        
    // Use a NumberFormatInfo object for IFormatProvider.
    Console::WriteLine( S"\nA NumberFormatInfo object with digit " 
        S"group size = 2 and \ndigit separator = '_' is used " 
        S"for the IFormatProvider:" );
    Console::WriteLine( format, S"'N' format string:", 
        singleValue.ToString( S"N", culture ) );
    Console::WriteLine( format, S"'E' format string:", 
        singleValue.ToString( S"E", culture ) );
}

/*
This example of
  Single::ToString( ),
  Single::ToString( String* ),
  Single::ToString( IFormatProvider* ), and
  Single::ToString( String*, IFormatProvider* )
generates the following output when run in the [en-US] culture.
A Single number is formatted with various combinations of format
strings and IFormatProvider.

IFormatProvider is not used; the default culture is [en-US]:
  No format string:             11876.54
  'N5' format string:           11,876.54000
  'E' format string:            1.187654E+004
  'E5' format string:           1.18765E+004

A CultureInfo object for [nl-NL] is used for the IFormatProvider:
  No format string:             11876,54
  'N5' format string:           11.876,54000
  'E' format string:            1,187654E+004

A NumberFormatInfo object with digit group size = 2 and
digit separator = '_' is used for the IFormatProvider:
  'N' format string:            1_18_76,54
  'E' format string:            1,187654E+004
*/ 

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

See Also

Single Structure | Single Members | System Namespace

Show:
© 2014 Microsoft