Export (0) Print
Expand All
0 out of 4 rated this helpful - Rate this topic

Double.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 15 digits of precision although a maximum of 17 digits is maintained internally. If the value of this instance has greater than 15 digits, ToString returns PositiveInfinitySymbol or NegativeInfinitySymbol instead of the expected number. If you require more precision, specify format with the "G17" format specification, which always returns 17 digits of precision, or "R", which returns 15 digits if the number can be represented with that precision or 17 digits if the number can only be represented with maximum precision.

The provider parameter is an IFormatProvider instance that obtains a NumberFormatInfo object 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 illustrates the use of ToString, taking a String and an IFormatProvider as parameters:

public class Temperature : IFormattable {
    /// <summary>
    /// IFormattable.ToString implementation.
    /// </summary>
    public string ToString(string format, IFormatProvider provider) {
        if( format != null ) {
            if( format.Equals("F") ) {
                return String.Format("{0}'F", this.Value.ToString());
            }
            if( format.Equals("C") ) {
                return String.Format("{0}'C", this.Celsius.ToString());
            }
        }

        return m_value.ToString(format, provider);
    }

    // The value holder
    protected double m_value;

    public double Value {
        get {
            return m_value;
        }
        set {
            m_value = value;
        }
    }

    public double Celsius {
        get {
            return (m_value-32.0)/1.8;
        }
        set {
            m_value = 1.8*value+32.0;
        }
    }
}

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
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.