Export (0) Print
Expand All

IFormattable.ToString Method

Formats the value of the current instance using the specified format.

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

'Declaration
Function ToString ( _
	format As String, _
	formatProvider As IFormatProvider _
) As String

Parameters

format
Type: System.String
The String specifying the format to use.
-or-
Nothing to use the default format defined for the type of the IFormattable implementation.
formatProvider
Type: System.IFormatProvider
The IFormatProvider to use to format the value.
-or-
Nothing to obtain the numeric format information from the current locale setting of the operating system.

Return Value

Type: System.String
A String containing the value of the current instance in the specified format.

NumberFormatInfo, DateTimeFormatInfo and CultureInfo implement the IFormatProvider interface.

NumberFormatInfo supplies numeric formatting information, such as the characters to use for decimal and thousand separators and the spelling and placement of currency symbols in monetary values.

DateTimeFormatInfo supplies date- and time-related formatting information, such as the position of the month, the day and the year in a date pattern.

CultureInfo contains the default formatting information in a specific culture, including the numeric format information and date- and time-related formatting information.

The following code example demonstrates implementing the ToString method. This code example is part of a larger example provided for the IFormattable class.


Public Overrides Function ToString() As String
   Return ToString(Nothing, Nothing)
End Function

Public Overloads Function ToString(ByVal format As String) As String
   Return ToString(format, Nothing)
End Function

Public Overloads Function ToString(ByVal format As String, _
                                   ByVal fp As IFormatProvider) As String _
        Implements IFormattable.ToString
   If String.IsNullOrEmpty(format) Then format = "G"

   ' If G format specifier is passed, display like this: (x, y).
   If format.ToLower() = "g" Then _
       Return String.Format("({0}, {1})", x, y)

   ' For "x" format specifier, return just the x value as a string
   If format.ToLower = "x" Then _
       Return x.ToString()

   ' For "y" format specifier, return just the y value as a string
   If format.ToLower = "y" Then _
       Return y.ToString()

   ' For any unrecognized format, throw an exception.
   Throw New FormatException(String.Format("Invalid format string: '{0}'.", format))
End Function


Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Community Additions

ADD
Show:
© 2014 Microsoft