共用方式為


Single.ToString Method (String)

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.

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

Syntax

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

Parameters

Return Value

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

Exceptions

Exception Condition
FormatException

format is invalid.

Remarks

The return value can be PositiveInfinitySymbol, NegativeInfinitySymbol, NaNSymbol, or the string representation of a number, 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 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 return value is formatted using the NumberFormatInfo object for the current culture. To apply the formatting conventions of a specified culture, call the Single.ToString(String, IFormatProvider) method.

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(String) 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 several Single values using each of the supported standard numeric format specifiers together with two custom numeric format strings. One of those custom format strings illustrates how to pad a Single value with leading zeroes. In converting the numeric values to strings, the example uses the formatting conventions of the en-US culture.

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.