Partager via


Single.ToString Method (String, IFormatProvider)

Microsoft Silverlight will reach end of support after October 2021. Learn more.

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)

Syntax

'Declaration
<SecuritySafeCriticalAttribute> _
Public Function ToString ( _
    format As String, _
    provider As IFormatProvider _
) As String
[SecuritySafeCriticalAttribute]
public string ToString(
    string format,
    IFormatProvider provider
)

Parameters

Return Value

Type: System.String
The string representation of the value of this instance as specified by format and provider.

Implements

IFormattable.ToString(String, IFormatProvider)

Remarks

The return value can be PositiveInfinitySymbol, NegativeInfinitySymbol, NaNSymbol, or the string representation of the value of the current instance, as specified by format.

The format parameter can be any valid standard numeric format specifier except for D and X, as well as any combination of custom numeric format specifiers. If format is nulla null reference (Nothing in Visual Basic) or an empty string, the return value for this instance is formatted with the general numeric format specifier ("G").

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

NoteNote:

Because the Single data type is not supported on the Macintosh OS X operating system, the string representation of a Single value may be different from those of the other .NET Framework numeric types that are supported by OS X.

The provider parameter is an IFormatProvider implementation whose GetFormat method returns a NumberFormatInfo object. Typically, provider is a CultureInfo object or a NumberFormatInfo object. The provider parameter supplies culture-specific information used in formatting. If provider is nulla null reference (Nothing in Visual Basic), the return value is formatted with the NumberFormatInfo object for the current culture.

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.

Examples

The following example displays a Single value using each of the supported standard numeric format specifiers for several different cultures.

Version Information

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

Platforms

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