Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

Single.ToString Method (String, IFormatProvider)

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

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

public string ToString (
	string format,
	IFormatProvider provider
)
public final String ToString (
	String format, 
	IFormatProvider provider
)
public final function ToString (
	format : String, 
	provider : IFormatProvider
) : String

Parameters

format

A format specification.

provider

An IFormatProvider that supplies culture-specific formatting information.

Return Value

The string representation of the value of this instance as specified by format and provider.

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

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

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.

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".

If format is a null reference (Nothing in Visual Basic) or an empty string, the return value for this instance is formatted with the general format specifier ("G").

By default, the return value only contains 7 digits of precision although a maximum of 9 digits is maintained internally. If the value of this instance has greater than 7 digits, ToString returns PositiveInfinitySymbol or NegativeInfinitySymbol instead of the expected number. If you require more precision, specify format with the "G9" format specification, which always returns 9 digits of precision, or "R", which returns 7 digits if the number can be represented with that precision or 9 digits if the number can only be represented with maximum precision.

The provider parameter is an IFormatProvider that obtains a NumberFormatInfo that supplies culture-specific format information. If provider is a null reference (Nothing in Visual Basic), the return value is formatted with NumberFormatInfo data for the current culture.

The following code example converts a Single value to a String with the ToString method, using a formatting String and an IFormatProvider object.

// 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
*/ 

// Example for the Single.ToString( ) methods.
import System.*;
import System.Globalization.*;
import System.IFormatProvider;

class SingleToStringDemo
{
    public static void main(String[] args)
    {
        float singleValue = 11876.54f;
        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.get_CurrentCulture().get_Name());

        // Format the number without and with format strings.
        Console.WriteLine("\nIFormatProvider is not " 
            + "used; the default culture is [{0}]:", 
            CultureInfo.get_CurrentCulture().get_Name());
        Console.WriteLine(format, "No format string:", 
            String.valueOf(singleValue));
        Console.WriteLine(format, "'N5' format string:", 
            ((System.Double)singleValue).ToString("N5"));
        Console.WriteLine(format, "'E' format string:", 
            ((System.Double)singleValue).ToString("E"));
        Console.WriteLine(format, "'E5' format string:", 
            ((System.Double)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.get_CurrentCulture().get_Name().
            Substring(0, 2).equalsIgnoreCase("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:", 
            String.valueOf(singleValue));
        Console.WriteLine(format, "'N5' format string:", 
            ((System.Double)singleValue).ToString("N5", culture));
        Console.WriteLine(format, "'E' format string:", 
            ((System.Double)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.get_NumberFormat();
        numInfo.set_NumberGroupSizes(new int[] { 2 });
        numInfo.set_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:", 
            ((System.Double)singleValue).ToString("N", culture));
        Console.WriteLine(format, "'E' format string:", 
            ((System.Double)singleValue).ToString("E", culture));
    } //main
} //SingleToStringDemo

/*
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.54004
  '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,54004
  '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
*/

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

Show:
© 2014 Microsoft