This documentation is archived and is not being maintained.

Double.ToString Method (IFormatProvider)

.NET Framework 1.1

Converts the numeric value of this instance to its equivalent string representation using the specified culture-specific format information.

[Visual Basic]
Overloads Public Overridable Function ToString( _
   ByVal provider As IFormatProvider _
) As String
[C#]
public virtual string ToString(
 IFormatProvider provider
);
[C++]
public: virtual String* ToString(
 IFormatProvider* provider
);
[JScript]
public function ToString(
   provider : IFormatProvider
) : String;

Parameters

provider
An IFormatProvider that supplies culture-specific formatting information.

Return Value

The string representation of the value of this instance as specified by provider.

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".

This instance is formatted with the general format specifier ("G").

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.

Example

The following code sample illustrates the use of ToString, taking a String and an IFormatProvider as parameters:

[Visual Basic] 
Public Class Temperature
    Implements IFormattable

    Public Overloads Function ToString(ByVal format As String, ByVal provider As IFormatProvider) As String _
        Implements IFormattable.ToString

        If Not (format Is Nothing) Then
            If format.Equals("F") Then
                Return [String].Format("{0}'F", Me.Value.ToString())
            End If
            If format.Equals("C") Then
                Return [String].Format("{0}'C", Me.Celsius.ToString())
            End If
        End If

        Return m_value.ToString(format, provider)
    End Function

    ' The value holder
    Protected m_value As Double

    Public Property Value() As Double
        Get
            Return m_value
        End Get
        Set(ByVal Value As Double)
            m_value = Value
        End Set
    End Property

    Public Property Celsius() As Double
        Get
            Return (m_value - 32) / 1.8
        End Get
        Set(ByVal Value As Double)
            m_value = Value * 1.8 + 32
        End Set
    End Property
End Class

[C#] 
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;
        }
    }
}

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

      return m_value.ToString(format, provider);
   }

   // The value holder
protected:
   double m_value;

public:
   __property double get_Value() {
      return m_value;
   }
   __property void set_Value( double value ) {
      m_value = value;
   }


   __property double get_Celsius() {
      return (m_value-32.0)/1.8;
   }
   __property void set_Celsius( double value ) {
      m_value = 1.8*value+32.0;
   }

};

[JScript] 
public class Temperature implements IFormattable {
    /// <summary>
    /// IFormattable.ToString implementation.
    /// </summary>
    public function ToString(format : String, provider : IFormatProvider) : String {
        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 var m_value : double;

    public function get Value() : double{
        return m_value;
    }
    
            public function set Value(value : double) {
        m_value = value;
    }

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

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

Double Structure | Double Members | System Namespace | Double.ToString Overload List | Formatting Overview | Parse | String

Show: