Converts the numeric value of this instance to its equivalent string representation, using the specified format.
[Visual Basic]
Overloads Public Function ToString( _
ByVal format As String _
) As String
[C#]
public string ToString(
string format
);
[C++]
public: String* ToString(
String* format
);
[JScript]
public function ToString(
format : String
) : String;
Parameters
- format
- A format string.
Return Value
The string representation of the value of this instance as specified by format.
Exceptions
Remarks
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 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 return value is formatted with NumberFormatInfo data for the current culture.
Example
[Visual Basic, C#, C++] The following code example converts a Single value to a String with the ToString method, using a formatting String.
[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
in the upper-left corner of the page.
Requirements
Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard
See Also
Single Structure | Single Members | System Namespace | Single.ToString Overload List | Formatting Overview | Numeric Format Strings | Parse | String