Double.ToString 메서드

정의

이 인스턴스의 숫자 값을 해당하는 문자열 표현으로 변환합니다.

오버로드

ToString()

이 인스턴스의 숫자 값을 해당하는 문자열 표현으로 변환합니다.

ToString(IFormatProvider)

지정된 문화권별 형식 정보를 사용하여 이 인스턴스의 숫자 값을 해당 문자열 표현으로 변환합니다.

ToString(String)

지정된 형식을 사용하여 이 인스턴스의 숫자 값을 해당 문자열 표현으로 변환합니다.

ToString(String, IFormatProvider)

지정된 형식 및 문화권별 형식 정보를 사용하여 이 인스턴스의 숫자 값을 해당 문자열 표현으로 변환합니다.

ToString()

이 인스턴스의 숫자 값을 해당하는 문자열 표현으로 변환합니다.

public:
 override System::String ^ ToString();
public override string ToString ();
override this.ToString : unit -> string
Public Overrides Function ToString () As String

반환

이 인스턴스 값의 문자열 표현입니다.

예제

다음 예제에서는 기본 Double.ToString() 메서드를 사용하여 여러 Double 값의 문자열 표현을 표시합니다.

double number;

number = 1.6E20;
// Displays 1.6E+20.
Console.WriteLine(number.ToString());

number = 1.6E2;
// Displays 160.
Console.WriteLine(number.ToString());

number = -3.541;
// Displays -3.541.
Console.WriteLine(number.ToString());

number = -1502345222199E-07;
// Displays -150234.5222199.
Console.WriteLine(number.ToString());

number = -15023452221990199574E-09;
// Displays -15023452221.9902.
Console.WriteLine(number.ToString());

number = .60344;
// Displays 0.60344.
Console.WriteLine(number.ToString());

number = .000000001;
// Displays 1E-09.
Console.WriteLine(number.ToString());
let number = 1.6E20
// Displays 1.6E+20.
printfn $"{number.ToString()}"

let number = 1.6E2
// Displays 160.
printfn $"{number.ToString()}"

let number = -3.541
// Displays -3.541.
printfn $"{number.ToString()}"

let number = -1502345222199E-07
// Displays -150234.5222199.
printfn $"{number.ToString()}"

let number = -15023452221990199574E-09
// Displays -15023452221.9902.
printfn $"{number.ToString()}"

let number = 0.60344
// Displays 0.60344.
printfn $"{number.ToString()}"

let number = 0.000000001
// Displays 1E-09.
printfn $"{number.ToString()}"
Dim number As Double

number = 1.6E20
' Displays 1.6E+20.      
Console.WriteLIne(number.ToString())

number = 1.6E2
' Displays 160.
Console.WriteLine(number.ToString())

number = -3.541
' Displays -3.541.
Console.WriteLine(number.ToString())

number = -1502345222199E-07
' Displays -150234.5222199.
Console.WriteLine(number.ToString())

number = -15023452221990199574E-09
' Displays -15023452221.9902.
Console.WriteLine(number.ToString())

number = .60344
' Displays 0.60344.
Console.WriteLine(number.ToString())

number = .000000001
' Displays 1E-09.
Console.WriteLine(number.ToString())

다음 예제에서는 ToString합니다.

bool done = false;
String^ inp;
do
{
   Console::Write( "Enter a real number: " );
   inp = Console::ReadLine();
   try
   {
      d = Double::Parse( inp );
      Console::WriteLine( "You entered {0}.", d );
      done = true;
   }
   catch ( FormatException^ ) 
   {
      Console::WriteLine( "You did not enter a number." );
   }
   catch ( Exception^ e ) 
   {
      Console::WriteLine( "An exception occurred while parsing your response: {0}", e );
   }

}
while (  !done );
bool done = false;
string inp;
do {
   Console.Write("Enter a real number: ");
   inp = Console.ReadLine();
   try {
      d = Double.Parse(inp);
      Console.WriteLine("You entered {0}.", d.ToString());
      done = true;
   }
   catch (FormatException) {
      Console.WriteLine("You did not enter a number.");
   }
   catch (ArgumentNullException) {
      Console.WriteLine("You did not supply any input.");
   }
   catch (OverflowException) {
       Console.WriteLine("The value you entered, {0}, is out of range.", inp);
   }
} while (!done);
let mutable completed = false
while not completed do
    printf "Enter a real number: "
    let inp = stdin.ReadLine()
    try
        let d = Double.Parse inp
        printfn $"You entered {d}."
        completed <- true
    with
    | :? FormatException ->
        printfn "You did not enter a number."
    | :? ArgumentNullException ->
        printfn "You did not supply any input."
    | :? OverflowException ->
         printfn $"The value you entered, {inp}, is out of range."
Dim Done As Boolean = False
Dim Inp As String
Do

    Console.Write("Enter a real number: ")
    inp = Console.ReadLine()
    Try
        D = Double.Parse(inp)
        Console.WriteLine("You entered " + D.ToString() + ".")
        Done = True
    Catch e As FormatException
        Console.WriteLine("You did not enter a number.")
    Catch e As ArgumentNullException
       Console.WriteLine("You did not supply any input.")
    Catch e As OverflowException
       Console.WriteLine("The value you entered, {0}, is out of range.", inp)      
    End Try
Loop While Not Done

설명

메서드는 ToString()Double 현재 문화권의 기본("G" 또는 일반) 형식으로 값의 형식을 지정합니다. 다른 형식, 전체 자릿수 또는 문화권을 지정하려면 다음과 같이 메서드의 ToString 다른 오버로드를 사용합니다.

형식을 사용하려면 문화권의 경우 오버로드 사용
기본("G") 형식 특정 문화권 ToString(IFormatProvider)
특정 형식 또는 전체 자릿수 기본(현재) 문화권 ToString(String)
특정 형식 또는 전체 자릿수 특정 문화권 ToString(String, IFormatProvider)

반환 값은 , , NegativeInfinitySymbolNaNSymbol또는 폼의 문자열일 PositiveInfinitySymbol수 있습니다.

[sign]정수 자릿수[.[ fractional-digits]][E[sign]exponential-digits]

선택적 요소는 대괄호([ 및 ])로 프레임됩니다. "digits"라는 용어가 포함된 요소는 0에서 9까지의 일련의 숫자 문자로 구성됩니다. 다음 표에 나열된 요소가 지원됩니다.

요소 설명
sign 음수 기호 또는 양수 기호입니다.
정수 자릿수 숫자의 정수 부분을 지정하는 일련의 숫자입니다. 소수 자릿수가 있는 경우 정수 자릿수가 없을 수 있습니다.
'.' 문화권별 소수점 기호입니다.
fractional-digits 숫자의 소수 부분을 지정하는 일련의 숫자입니다.
'E' 지수(과학적) 표기법을 나타내는 대문자 'E'입니다.
exponential-digits 지수를 지정하는 일련의 숫자입니다.

반환 값의 예로는 "100", "-123,456,789", "123.45E+6", "500", "3.1416", "600", "-0.123" 및 "-Infinity"가 있습니다.

.NET은 다음과 같은 서식 지정 topics 자세히 설명하는 광범위한 서식 지정 지원을 제공합니다.

추가 정보

적용 대상

ToString(IFormatProvider)

지정된 문화권별 형식 정보를 사용하여 이 인스턴스의 숫자 값을 해당 문자열 표현으로 변환합니다.

public:
 virtual System::String ^ ToString(IFormatProvider ^ provider);
public:
 System::String ^ ToString(IFormatProvider ^ provider);
public string ToString (IFormatProvider provider);
public string ToString (IFormatProvider? provider);
override this.ToString : IFormatProvider -> string
Public Function ToString (provider As IFormatProvider) As String

매개 변수

provider
IFormatProvider

문화권별 형식 정보를 제공하는 개체입니다.

반환

이 인스턴스의 값을 provider에 지정된 내용에 따라 나타낸 문자열 표현입니다.

구현

예제

다음 예제에서는 여러 다른 문화권을 나타내는 개체를 사용하여 CultureInfoDouble 값의 문자열 표현을 표시합니다.

double value;

value = -16325.62015;
// Display value using the invariant culture.
Console.WriteLine(value.ToString(CultureInfo.InvariantCulture));
// Display value using the en-GB culture.
Console.WriteLine(value.ToString(CultureInfo.CreateSpecificCulture("en-GB")));
// Display value using the de-DE culture.
Console.WriteLine(value.ToString(CultureInfo.CreateSpecificCulture("de-DE")));

value = 16034.125E21;
// Display value using the invariant culture.
Console.WriteLine(value.ToString(CultureInfo.InvariantCulture));
// Display value using the en-GB culture.
Console.WriteLine(value.ToString(CultureInfo.CreateSpecificCulture("en-GB")));
// Display value using the de-DE culture.
Console.WriteLine(value.ToString(CultureInfo.CreateSpecificCulture("de-DE")));
// This example displays the following output to the console:
//       -16325.62015
//       -16325.62015
//       -16325,62015
//       1.6034125E+25
//       1.6034125E+25
//       1,6034125E+25
let value = -16325.62015
// Display value using the invariant culture.
printfn $"{value.ToString CultureInfo.InvariantCulture}"
// Display value using the en-GB culture.
printfn $"""{value.ToString(CultureInfo.CreateSpecificCulture "en-GB")}"""
// Display value using the de-DE culture.
printfn $"""{value.ToString(CultureInfo.CreateSpecificCulture "de-DE")}"""

let value = 16034.125E21
// Display value using the invariant culture.
printfn $"{value.ToString CultureInfo.InvariantCulture}"
// Display value using the en-GB culture.
printfn $"""{value.ToString(CultureInfo.CreateSpecificCulture "en-GB")}"""
// Display value using the de-DE culture.
printfn $"""{value.ToString(CultureInfo.CreateSpecificCulture "de-DE")}"""
// This example displays the following output to the console:
//       -16325.62015
//       -16325.62015
//       -16325,62015
//       1.6034125E+25
//       1.6034125E+25
//       1,6034125E+25
Dim value As Double 

value = -16325.62015
' Display value using the invariant culture.
Console.WriteLine(value.ToString(CultureInfo.InvariantCulture))
' Display value using the en-GB culture.
Console.WriteLine(value.ToString(CultureInfo.CreateSpecificCulture("en-GB")))
' Display value using the de-DE culture.
Console.WriteLine(value.ToString(CultureInfo.CreateSpecificCulture("de-DE")))

value = 16034.125E21
' Display value using the invariant culture.
Console.WriteLine(value.ToString(CultureInfo.InvariantCulture))
' Display value using the en-GB culture.
Console.WriteLine(value.ToString(CultureInfo.CreateSpecificCulture("en-GB")))
' Display value using the de-DE culture.
Console.WriteLine(value.ToString(CultureInfo.CreateSpecificCulture("de-DE")))
' This example displays the following output to the console:
'       -16325.62015
'       -16325.62015
'       -16325,62015
'       1.6034125E+25
'       1.6034125E+25
'       1,6034125E+25

다음 예제에서는 및 를 매개 변수로 사용하여 StringIFormatProvider 를 사용하는 ToString것을 보여 줍니다.

public ref class Temperature: public IFormattable
{
   // IFormattable.ToString implementation.
public:
   virtual String^ ToString( String^ format, IFormatProvider^ provider )
   {
      if ( format != nullptr )
      {
         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 );
   }

protected:
   // The value holder
   double m_value;

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

   property double Celsius 
   {
      double get()
      {
         return (m_value - 32.0) / 1.8;
      }
      void set( double value )
      {
         m_value = 1.8 * value + 32.0;
      }
   }
};
public class Temperature : IFormattable {
    // IFormattable.ToString implementation.
    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;
        }
    }
}
type Temperature() =
    member val Value = 0. with get, set

    member this.Celsius
        with get () =
            (this.Value - 32.) / 1.8
        and set (value) =
            this.Value <- 1.8 * value + 32.

    // IFormattable.ToString implementation.
    interface IFormattable with
        // IFormattable.ToString implementation.
        member this.ToString(format: string, provider: IFormatProvider) =
            match format with
            | "F" ->
                $"{this.Value}'F"
            | "C" ->
                $"{this.Celsius}'C"
            | _ ->
                this.Value.ToString(format, provider)
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

설명

메서드는 ToString(IFormatProvider)Double 지정된 문화권의 기본("G" 또는 일반) 형식으로 값의 형식을 지정합니다. 다른 형식이나 문화권을 지정하려면 다음과 같이 메서드의 ToString 다른 오버로드를 사용합니다.

형식을 사용하려면 문화권의 경우 오버로드 사용
기본("G") 형식 기본값(현재) ToString()
특정 형식 또는 전체 자릿수 기본(현재) 문화권 ToString(String)
특정 형식 또는 전체 자릿수 특정 문화권 ToString(String, IFormatProvider)

반환 값은 , , NegativeInfinitySymbolNaNSymbol또는 폼의 문자열일 PositiveInfinitySymbol수 있습니다.

[sign]정수 자릿수[.[ fractional-digits]][E[sign]exponential-digits]

선택적 요소는 대괄호([ 및 ])로 프레임됩니다. "digits"라는 용어가 포함된 요소는 0에서 9까지의 일련의 숫자 문자로 구성됩니다. 다음 표에 나열된 요소가 지원됩니다.

요소 설명
sign 음수 기호 또는 양수 기호입니다.
정수 자릿수 숫자의 정수 부분을 지정하는 일련의 숫자입니다. 소수 자릿수가 있는 경우 정수 자릿수가 없을 수 있습니다.
'.' 문화권별 소수점 기호입니다.
fractional-digits 숫자의 소수 부분을 지정하는 일련의 숫자입니다.
'E' 지수(과학적) 표기법을 나타내는 대문자 'E'입니다.
exponential-digits 지수를 지정하는 일련의 숫자입니다.

반환 값의 예로는 "100", "-123,456,789", "123.45E+6", "500", "3.1416", "600", "-0.123" 및 "-Infinity"가 있습니다.

이 instance 일반 숫자 형식 지정자("G")로 서식이 지정됩니다.

.NET은 다음과 같은 서식 지정 topics 자세히 설명하는 광범위한 서식 지정 지원을 제공합니다.

provider 매개 변수는 메서드가 개체를 IFormatProvider 반환하는 GetFormat 구현입니다NumberFormatInfo. 일반적으로 는 providerCultureInfo 개체 또는 개체입니다 NumberFormatInfo . 매개 변수는 provider 서식 지정에 사용되는 문화권별 정보를 제공합니다. 가 이nullprovider 반환 값은 현재 문화권에 대한 개체를 NumberFormatInfo 사용하여 형식이 지정됩니다.

추가 정보

적용 대상

ToString(String)

지정된 형식을 사용하여 이 인스턴스의 숫자 값을 해당 문자열 표현으로 변환합니다.

public:
 System::String ^ ToString(System::String ^ format);
public string ToString (string format);
public string ToString (string? format);
override this.ToString : string -> string
Public Function ToString (format As String) As String

매개 변수

format
String

숫자 서식 문자열입니다.

반환

이 인스턴스의 값을 format에 지정된 내용에 따라 나타낸 문자열 표현입니다.

예외

format이 잘못되었습니다.

예제

다음 예제에서는 숫자 값을 정의하고 "C" 표준 숫자 형식 문자열을 사용하여 통화 값으로, "N" 표준 숫자 형식 문자열을 사용하여 소수 자릿수 3개의 숫자 값으로 형식을 지정합니다. 결과 문자열은 en-US 문화권의 규칙을 사용하여 형식이 지정됩니다. 숫자 형식 문자열에 대한 자세한 내용은 표준 숫자 서식 문자열 및사용자 지정 숫자 형식 문자열을 참조하세요.

using System;

public class Example
{
   public static void Main()
   {
      float number = 1764.3789m;

      // Format as a currency value.
      Console.WriteLine(number.ToString("C"));

      // Format as a numeric value with 3 decimal places.
      Console.WriteLine(number.ToString("N3"));
   }
}
// The example displays the following output:
//       $1,764.38
//       1,764.379
open System

let number = 1764.3789m

// Format as a currency value.
printfn $"""{number.ToString "C"}"""

// Format as a numeric value with 3 decimal places.
printfn $"""{number.ToString "N3"}"""

// The example displays the following output:
//       $1,764.38
//       1,764.379
Module Example
   Public Sub Main()
      Dim number As Double = 1764.3789
      
      ' Format as a currency value.
      Console.WriteLine(number.ToString("C"))
      
      ' Format as a numeric value with 3 decimal places.
      Console.WriteLine(number.ToString("N3"))
   End Sub
End Module
' The example displays the following output:
'       $1,764.38
'       1,764.379

다음 예제에서는 지원되는 표준 숫자 형식 지정자와 세 개의 사용자 지정 숫자 형식 문자열을 사용하여 여러 Double 값을 표시합니다. 이러한 사용자 지정 형식 문자열 중 하나는 앞에 오는 0으로 값을 패딩하는 Single 방법을 보여 줍니다. 또한 이 예제에서는 "R"을 제외한 각 표준 형식 지정자와 함께 전체 자릿수 지정자를 사용합니다. 전체 자릿수 지정자의 값은 0에서 3까지입니다. 숫자 값을 문자열로 변환하기 위해 이 예제에서는 en-US 문화권의 서식 지정 규칙을 사용합니다.

using namespace System;

void main()
{
   array<Double>^ numbers= {1054.32179, -195489100.8377, 1.0437E21, 
                            -1.0573e-05};
   array<String^>^ specifiers = { "C", "E", "e", "F", "G", "N", "P", 
                                  "R", "#,000.000", "0.###E-000",
                                  "000,000,000,000.00###" };
   for each (Double number in numbers)
   {  
      Console::WriteLine("Formatting of {0}:", number);
      for each (String^ specifier in specifiers) {
         Console::WriteLine("   {0,-22} {1}", 
                            specifier + ":", number.ToString(specifier));
         // Add precision specifiers from 0 to 3.
         if (specifier->Length == 1 & ! specifier->Equals("R")) {
            for (int precision = 0; precision <= 3; precision++) {
               String^ pSpecifier = String::Format("{0}{1}", specifier, precision);
               Console::WriteLine("   {0,-22} {1}", 
                                  pSpecifier + ":", number.ToString(pSpecifier));
            }    
            Console::WriteLine();
         }   
      }
      Console::WriteLine();
   }
}
// The example displays the following output:
//       Formatting of 1054.32179:
//          C:                     $1,054.32
//          C0:                    $1,054
//          C1:                    $1,054.3
//          C2:                    $1,054.32
//          C3:                    $1,054.322
//       
//          E:                     1.054322E+003
//          E0:                    1E+003
//          E1:                    1.1E+003
//          E2:                    1.05E+003
//          E3:                    1.054E+003
//       
//          e:                     1.054322e+003
//          e0:                    1e+003
//          e1:                    1.1e+003
//          e2:                    1.05e+003
//          e3:                    1.054e+003
//       
//          F:                     1054.32
//          F0:                    1054
//          F1:                    1054.3
//          F2:                    1054.32
//          F3:                    1054.322
//       
//          G:                     1054.32179
//          G0:                    1054.32179
//          G1:                    1E+03
//          G2:                    1.1E+03
//          G3:                    1.05E+03
//       
//          N:                     1,054.32
//          N0:                    1,054
//          N1:                    1,054.3
//          N2:                    1,054.32
//          N3:                    1,054.322
//       
//          P:                     105,432.18 %
//          P0:                    105,432 %
//          P1:                    105,432.2 %
//          P2:                    105,432.18 %
//          P3:                    105,432.179 %
//       
//          R:                     1054.32179
//          #,000.000:             1,054.322
//          0.###E-000:            1.054E003
//          000,000,000,000.00###: 000,000,001,054.32179
//       
//       Formatting of -195489100.8377:
//          C:                     ($195,489,100.84)
//          C0:                    ($195,489,101)
//          C1:                    ($195,489,100.8)
//          C2:                    ($195,489,100.84)
//          C3:                    ($195,489,100.838)
//       
//          E:                     -1.954891E+008
//          E0:                    -2E+008
//          E1:                    -2.0E+008
//          E2:                    -1.95E+008
//          E3:                    -1.955E+008
//       
//          e:                     -1.954891e+008
//          e0:                    -2e+008
//          e1:                    -2.0e+008
//          e2:                    -1.95e+008
//          e3:                    -1.955e+008
//       
//          F:                     -195489100.84
//          F0:                    -195489101
//          F1:                    -195489100.8
//          F2:                    -195489100.84
//          F3:                    -195489100.838
//       
//          G:                     -195489100.8377
//          G0:                    -195489100.8377
//          G1:                    -2E+08
//          G2:                    -2E+08
//          G3:                    -1.95E+08
//
//          N:                     -195,489,100.84
//          N0:                    -195,489,101
//          N1:                    -195,489,100.8
//          N2:                    -195,489,100.84
//          N3:                    -195,489,100.838
//
//          P:                     -19,548,910,083.77 %
//          P0:                    -19,548,910,084 %
//          P1:                    -19,548,910,083.8 %
//          P2:                    -19,548,910,083.77 %
//          P3:                    -19,548,910,083.770 %
//
//          R:                     -195489100.8377
//          #,000.000:             -195,489,100.838
//          0.###E-000:            -1.955E008
//          000,000,000,000.00###: -000,195,489,100.8377
//
//       Formatting of 1.0437E+21:
//          C:                     $1,043,700,000,000,000,000,000.00
//          C0:                    $1,043,700,000,000,000,000,000
//          C1:                    $1,043,700,000,000,000,000,000.0
//          C2:                    $1,043,700,000,000,000,000,000.00
//          C3:                    $1,043,700,000,000,000,000,000.000
//
//          E:                     1.043700E+021
//          E0:                    1E+021
//          E1:                    1.0E+021
//          E2:                    1.04E+021
//          E3:                    1.044E+021
//
//          e:                     1.043700e+021
//          e0:                    1e+021
//          e1:                    1.0e+021
//          e2:                    1.04e+021
//          e3:                    1.044e+021
//
//          F:                     1043700000000000000000.00
//          F0:                    1043700000000000000000
//          F1:                    1043700000000000000000.0
//          F2:                    1043700000000000000000.00
//          F3:                    1043700000000000000000.000
//
//          G:                     1.0437E+21
//          G0:                    1.0437E+21
//          G1:                    1E+21
//          G2:                    1E+21
//          G3:                    1.04E+21
//
//          N:                     1,043,700,000,000,000,000,000.00
//          N0:                    1,043,700,000,000,000,000,000
//          N1:                    1,043,700,000,000,000,000,000.0
//          N2:                    1,043,700,000,000,000,000,000.00
//          N3:                    1,043,700,000,000,000,000,000.000
//
//          P:                     104,370,000,000,000,000,000,000.00 %
//          P0:                    104,370,000,000,000,000,000,000 %
//          P1:                    104,370,000,000,000,000,000,000.0 %
//          P2:                    104,370,000,000,000,000,000,000.00 %
//          P3:                    104,370,000,000,000,000,000,000.000 %
//
//          R:                     1.0437E+21
//          #,000.000:             1,043,700,000,000,000,000,000.000
//          0.###E-000:            1.044E021
//          000,000,000,000.00###: 1,043,700,000,000,000,000,000.00
//
//       Formatting of -1.0573E-05:
//          C:                     $0.00
//          C0:                    $0
//          C1:                    $0.0
//          C2:                    $0.00
//          C3:                    $0.000
//
//          E:                     -1.057300E-005
//          E0:                    -1E-005
//          E1:                    -1.1E-005
//          E2:                    -1.06E-005
//          E3:                    -1.057E-005
//
//          e:                     -1.057300e-005
//          e0:                    -1e-005
//          e1:                    -1.1e-005
//          e2:                    -1.06e-005
//          e3:                    -1.057e-005
//
//          F:                     0.00
//          F0:                    0
//          F1:                    0.0
//          F2:                    0.00
//          F3:                    0.000
//
//          G:                     -1.0573E-05
//          G0:                    -1.0573E-05
//          G1:                    -1E-05
//          G2:                    -1.1E-05
//          G3:                    -1.06E-05
//
//          N:                     0.00
//          N0:                    0
//          N1:                    0.0
//          N2:                    0.00
//          N3:                    0.000
//
//          P:                     0.00 %
//          P0:                    0 %
//          P1:                    0.0 %
//          P2:                    0.00 %
//          P3:                    -0.001 %
//
//          R:                     -1.0573E-05
//          #,000.000:             000.000
//          0.###E-000:            -1.057E-005
//          000,000,000,000.00###: -000,000,000,000.00001
double[] numbers= {1054.32179, -195489100.8377, 1.0437E21,
                   -1.0573e-05};
string[] specifiers = { "C", "E", "e", "F", "G", "N", "P",
                        "R", "#,000.000", "0.###E-000",
                        "000,000,000,000.00###" };
foreach (double number in numbers)
{
   Console.WriteLine("Formatting of {0}:", number);
   foreach (string specifier in specifiers) {
      Console.WriteLine("   {0,-22} {1}",
                        specifier + ":", number.ToString(specifier));
      // Add precision specifiers from 0 to 3.
      if (specifier.Length == 1 & ! specifier.Equals("R")) {
         for (int precision = 0; precision <= 3; precision++) {
            string pSpecifier = String.Format("{0}{1}", specifier, precision);
            Console.WriteLine("   {0,-22} {1}",
                              pSpecifier + ":", number.ToString(pSpecifier));
         }
         Console.WriteLine();
      }
   }
   Console.WriteLine();
}
// The example displays the following output to the console:
//       Formatting of 1054.32179:
//          C:                     $1,054.32
//          C0:                    $1,054
//          C1:                    $1,054.3
//          C2:                    $1,054.32
//          C3:                    $1,054.322
//
//          E:                     1.054322E+003
//          E0:                    1E+003
//          E1:                    1.1E+003
//          E2:                    1.05E+003
//          E3:                    1.054E+003
//
//          e:                     1.054322e+003
//          e0:                    1e+003
//          e1:                    1.1e+003
//          e2:                    1.05e+003
//          e3:                    1.054e+003
//
//          F:                     1054.32
//          F0:                    1054
//          F1:                    1054.3
//          F2:                    1054.32
//          F3:                    1054.322
//
//          G:                     1054.32179
//          G0:                    1054.32179
//          G1:                    1E+03
//          G2:                    1.1E+03
//          G3:                    1.05E+03
//
//          N:                     1,054.32
//          N0:                    1,054
//          N1:                    1,054.3
//          N2:                    1,054.32
//          N3:                    1,054.322
//
//          P:                     105,432.18 %
//          P0:                    105,432 %
//          P1:                    105,432.2 %
//          P2:                    105,432.18 %
//          P3:                    105,432.179 %
//
//          R:                     1054.32179
//          #,000.000:             1,054.322
//          0.###E-000:            1.054E003
//          000,000,000,000.00###: 000,000,001,054.32179
//
//       Formatting of -195489100.8377:
//          C:                     ($195,489,100.84)
//          C0:                    ($195,489,101)
//          C1:                    ($195,489,100.8)
//          C2:                    ($195,489,100.84)
//          C3:                    ($195,489,100.838)
//
//          E:                     -1.954891E+008
//          E0:                    -2E+008
//          E1:                    -2.0E+008
//          E2:                    -1.95E+008
//          E3:                    -1.955E+008
//
//          e:                     -1.954891e+008
//          e0:                    -2e+008
//          e1:                    -2.0e+008
//          e2:                    -1.95e+008
//          e3:                    -1.955e+008
//
//          F:                     -195489100.84
//          F0:                    -195489101
//          F1:                    -195489100.8
//          F2:                    -195489100.84
//          F3:                    -195489100.838
//
//          G:                     -195489100.8377
//          G0:                    -195489100.8377
//          G1:                    -2E+08
//          G2:                    -2E+08
//          G3:                    -1.95E+08
//
//          N:                     -195,489,100.84
//          N0:                    -195,489,101
//          N1:                    -195,489,100.8
//          N2:                    -195,489,100.84
//          N3:                    -195,489,100.838
//
//          P:                     -19,548,910,083.77 %
//          P0:                    -19,548,910,084 %
//          P1:                    -19,548,910,083.8 %
//          P2:                    -19,548,910,083.77 %
//          P3:                    -19,548,910,083.770 %
//
//          R:                     -195489100.8377
//          #,000.000:             -195,489,100.838
//          0.###E-000:            -1.955E008
//          000,000,000,000.00###: -000,195,489,100.8377
//
//       Formatting of 1.0437E+21:
//          C:                     $1,043,700,000,000,000,000,000.00
//          C0:                    $1,043,700,000,000,000,000,000
//          C1:                    $1,043,700,000,000,000,000,000.0
//          C2:                    $1,043,700,000,000,000,000,000.00
//          C3:                    $1,043,700,000,000,000,000,000.000
//
//          E:                     1.043700E+021
//          E0:                    1E+021
//          E1:                    1.0E+021
//          E2:                    1.04E+021
//          E3:                    1.044E+021
//
//          e:                     1.043700e+021
//          e0:                    1e+021
//          e1:                    1.0e+021
//          e2:                    1.04e+021
//          e3:                    1.044e+021
//
//          F:                     1043700000000000000000.00
//          F0:                    1043700000000000000000
//          F1:                    1043700000000000000000.0
//          F2:                    1043700000000000000000.00
//          F3:                    1043700000000000000000.000
//
//          G:                     1.0437E+21
//          G0:                    1.0437E+21
//          G1:                    1E+21
//          G2:                    1E+21
//          G3:                    1.04E+21
//
//          N:                     1,043,700,000,000,000,000,000.00
//          N0:                    1,043,700,000,000,000,000,000
//          N1:                    1,043,700,000,000,000,000,000.0
//          N2:                    1,043,700,000,000,000,000,000.00
//          N3:                    1,043,700,000,000,000,000,000.000
//
//          P:                     104,370,000,000,000,000,000,000.00 %
//          P0:                    104,370,000,000,000,000,000,000 %
//          P1:                    104,370,000,000,000,000,000,000.0 %
//          P2:                    104,370,000,000,000,000,000,000.00 %
//          P3:                    104,370,000,000,000,000,000,000.000 %
//
//          R:                     1.0437E+21
//          #,000.000:             1,043,700,000,000,000,000,000.000
//          0.###E-000:            1.044E021
//          000,000,000,000.00###: 1,043,700,000,000,000,000,000.00
//
//       Formatting of -1.0573E-05:
//          C:                     $0.00
//          C0:                    $0
//          C1:                    $0.0
//          C2:                    $0.00
//          C3:                    $0.000
//
//          E:                     -1.057300E-005
//          E0:                    -1E-005
//          E1:                    -1.1E-005
//          E2:                    -1.06E-005
//          E3:                    -1.057E-005
//
//          e:                     -1.057300e-005
//          e0:                    -1e-005
//          e1:                    -1.1e-005
//          e2:                    -1.06e-005
//          e3:                    -1.057e-005
//
//          F:                     0.00
//          F0:                    0
//          F1:                    0.0
//          F2:                    0.00
//          F3:                    0.000
//
//          G:                     -1.0573E-05
//          G0:                    -1.0573E-05
//          G1:                    -1E-05
//          G2:                    -1.1E-05
//          G3:                    -1.06E-05
//
//          N:                     0.00
//          N0:                    0
//          N1:                    0.0
//          N2:                    0.00
//          N3:                    0.000
//
//          P:                     0.00 %
//          P0:                    0 %
//          P1:                    0.0 %
//          P2:                    0.00 %
//          P3:                    -0.001 %
//
//          R:                     -1.0573E-05
//          #,000.000:             000.000
//          0.###E-000:            -1.057E-005
//          000,000,000,000.00###: -000,000,000,000.00001
let numbers =
    [| 1054.32179; -195489100.8377; 1.0437E21; -1.0573e-05 |]
let specifiers =
    [| "C"; "E"; "e"; "F"; "G"; "N"; "P"
       "R"; "#,000.000"; "0.###E-000"
       "000,000,000,000.00###" |]
for number in numbers do
    printfn $"Formatting of {number}:"
    for specifier in specifiers do
        printfn $"""   {specifier + ":",-22} {number.ToString specifier}"""
        // Add precision specifiers from 0 to 3.
        if specifier.Length = 1 && not (specifier.Equals "R") then
            for precision = 0 to 3 do
                let pSpecifier = $"{specifier}{precision}"
                printfn $"""   {pSpecifier + ":",-22} {number.ToString pSpecifier}"""
            printfn ""
    printfn ""
// The example displays the following output to the console:
//       Formatting of 1054.32179:
//          C:                     $1,054.32
//          C0:                    $1,054
//          C1:                    $1,054.3
//          C2:                    $1,054.32
//          C3:                    $1,054.322
//
//          E:                     1.054322E+003
//          E0:                    1E+003
//          E1:                    1.1E+003
//          E2:                    1.05E+003
//          E3:                    1.054E+003
//
//          e:                     1.054322e+003
//          e0:                    1e+003
//          e1:                    1.1e+003
//          e2:                    1.05e+003
//          e3:                    1.054e+003
//
//          F:                     1054.32
//          F0:                    1054
//          F1:                    1054.3
//          F2:                    1054.32
//          F3:                    1054.322
//
//          G:                     1054.32179
//          G0:                    1054.32179
//          G1:                    1E+03
//          G2:                    1.1E+03
//          G3:                    1.05E+03
//
//          N:                     1,054.32
//          N0:                    1,054
//          N1:                    1,054.3
//          N2:                    1,054.32
//          N3:                    1,054.322
//
//          P:                     105,432.18 %
//          P0:                    105,432 %
//          P1:                    105,432.2 %
//          P2:                    105,432.18 %
//          P3:                    105,432.179 %
//
//          R:                     1054.32179
//          #,000.000:             1,054.322
//          0.###E-000:            1.054E003
//          000,000,000,000.00###: 000,000,001,054.32179
//
//       Formatting of -195489100.8377:
//          C:                     ($195,489,100.84)
//          C0:                    ($195,489,101)
//          C1:                    ($195,489,100.8)
//          C2:                    ($195,489,100.84)
//          C3:                    ($195,489,100.838)
//
//          E:                     -1.954891E+008
//          E0:                    -2E+008
//          E1:                    -2.0E+008
//          E2:                    -1.95E+008
//          E3:                    -1.955E+008
//
//          e:                     -1.954891e+008
//          e0:                    -2e+008
//          e1:                    -2.0e+008
//          e2:                    -1.95e+008
//          e3:                    -1.955e+008
//
//          F:                     -195489100.84
//          F0:                    -195489101
//          F1:                    -195489100.8
//          F2:                    -195489100.84
//          F3:                    -195489100.838
//
//          G:                     -195489100.8377
//          G0:                    -195489100.8377
//          G1:                    -2E+08
//          G2:                    -2E+08
//          G3:                    -1.95E+08
//
//          N:                     -195,489,100.84
//          N0:                    -195,489,101
//          N1:                    -195,489,100.8
//          N2:                    -195,489,100.84
//          N3:                    -195,489,100.838
//
//          P:                     -19,548,910,083.77 %
//          P0:                    -19,548,910,084 %
//          P1:                    -19,548,910,083.8 %
//          P2:                    -19,548,910,083.77 %
//          P3:                    -19,548,910,083.770 %
//
//          R:                     -195489100.8377
//          #,000.000:             -195,489,100.838
//          0.###E-000:            -1.955E008
//          000,000,000,000.00###: -000,195,489,100.8377
//
//       Formatting of 1.0437E+21:
//          C:                     $1,043,700,000,000,000,000,000.00
//          C0:                    $1,043,700,000,000,000,000,000
//          C1:                    $1,043,700,000,000,000,000,000.0
//          C2:                    $1,043,700,000,000,000,000,000.00
//          C3:                    $1,043,700,000,000,000,000,000.000
//
//          E:                     1.043700E+021
//          E0:                    1E+021
//          E1:                    1.0E+021
//          E2:                    1.04E+021
//          E3:                    1.044E+021
//
//          e:                     1.043700e+021
//          e0:                    1e+021
//          e1:                    1.0e+021
//          e2:                    1.04e+021
//          e3:                    1.044e+021
//
//          F:                     1043700000000000000000.00
//          F0:                    1043700000000000000000
//          F1:                    1043700000000000000000.0
//          F2:                    1043700000000000000000.00
//          F3:                    1043700000000000000000.000
//
//          G:                     1.0437E+21
//          G0:                    1.0437E+21
//          G1:                    1E+21
//          G2:                    1E+21
//          G3:                    1.04E+21
//
//          N:                     1,043,700,000,000,000,000,000.00
//          N0:                    1,043,700,000,000,000,000,000
//          N1:                    1,043,700,000,000,000,000,000.0
//          N2:                    1,043,700,000,000,000,000,000.00
//          N3:                    1,043,700,000,000,000,000,000.000
//
//          P:                     104,370,000,000,000,000,000,000.00 %
//          P0:                    104,370,000,000,000,000,000,000 %
//          P1:                    104,370,000,000,000,000,000,000.0 %
//          P2:                    104,370,000,000,000,000,000,000.00 %
//          P3:                    104,370,000,000,000,000,000,000.000 %
//
//          R:                     1.0437E+21
//          #,000.000:             1,043,700,000,000,000,000,000.000
//          0.###E-000:            1.044E021
//          000,000,000,000.00###: 1,043,700,000,000,000,000,000.00
//
//       Formatting of -1.0573E-05:
//          C:                     $0.00
//          C0:                    $0
//          C1:                    $0.0
//          C2:                    $0.00
//          C3:                    $0.000
//
//          E:                     -1.057300E-005
//          E0:                    -1E-005
//          E1:                    -1.1E-005
//          E2:                    -1.06E-005
//          E3:                    -1.057E-005
//
//          e:                     -1.057300e-005
//          e0:                    -1e-005
//          e1:                    -1.1e-005
//          e2:                    -1.06e-005
//          e3:                    -1.057e-005
//
//          F:                     0.00
//          F0:                    0
//          F1:                    0.0
//          F2:                    0.00
//          F3:                    0.000
//
//          G:                     -1.0573E-05
//          G0:                    -1.0573E-05
//          G1:                    -1E-05
//          G2:                    -1.1E-05
//          G3:                    -1.06E-05
//
//          N:                     0.00
//          N0:                    0
//          N1:                    0.0
//          N2:                    0.00
//          N3:                    0.000
//
//          P:                     0.00 %
//          P0:                    0 %
//          P1:                    0.0 %
//          P2:                    0.00 %
//          P3:                    -0.001 %
//
//          R:                     -1.0573E-05
//          #,000.000:             000.000
//          0.###E-000:            -1.057E-005
//          000,000,000,000.00###: -000,000,000,000.00001
Dim numbers() As Double = {1054.32179, -195489100.8377, 1.0437E21, _
                         -1.0573e-05}
Dim specifiers() As String = { "C", "E", "e", "F", "G", "N", "P", _
                               "R", "#,000.000", "0.###E-000", _
                               "000,000,000,000.00###" }
For Each number As Double In numbers
   Console.WriteLine("Formatting of {0}:", number)
   For Each specifier As String In specifiers
      Console.WriteLine("   {0,-22} {1}", 
                        specifier + ":", number.ToString(specifier))
      ' Add precision specifiers from 0 to 3.
      If specifier.Length = 1 And Not specifier.Equals("R") Then
         For precision As Integer = 0 To 3
            Dim pSpecifier As String = String.Format("{0}{1}", specifier, precision)
            Console.WriteLine("   {0,-22} {1}", 
                              pSpecifier + ":", number.ToString(pSpecifier))
         Next
         Console.WriteLine()      
      End If
   Next
   Console.WriteLine()
Next
' The example displays the following output:
'       Formatting of 1054.32179:
'          C:                     $1,054.32
'          C0:                    $1,054
'          C1:                    $1,054.3
'          C2:                    $1,054.32
'          C3:                    $1,054.322
'       
'          E:                     1.054322E+003
'          E0:                    1E+003
'          E1:                    1.1E+003
'          E2:                    1.05E+003
'          E3:                    1.054E+003
'       
'          e:                     1.054322e+003
'          e0:                    1e+003
'          e1:                    1.1e+003
'          e2:                    1.05e+003
'          e3:                    1.054e+003
'       
'          F:                     1054.32
'          F0:                    1054
'          F1:                    1054.3
'          F2:                    1054.32
'          F3:                    1054.322
'       
'          G:                     1054.32179
'          G0:                    1054.32179
'          G1:                    1E+03
'          G2:                    1.1E+03
'          G3:                    1.05E+03
'       
'          N:                     1,054.32
'          N0:                    1,054
'          N1:                    1,054.3
'          N2:                    1,054.32
'          N3:                    1,054.322
'       
'          P:                     105,432.18 %
'          P0:                    105,432 %
'          P1:                    105,432.2 %
'          P2:                    105,432.18 %
'          P3:                    105,432.179 %
'       
'          R:                     1054.32179
'          #,000.000:             1,054.322
'          0.###E-000:            1.054E003
'          000,000,000,000.00###: 000,000,001,054.32179
'       
'       Formatting of -195489100.8377:
'          C:                     ($195,489,100.84)
'          C0:                    ($195,489,101)
'          C1:                    ($195,489,100.8)
'          C2:                    ($195,489,100.84)
'          C3:                    ($195,489,100.838)
'       
'          E:                     -1.954891E+008
'          E0:                    -2E+008
'          E1:                    -2.0E+008
'          E2:                    -1.95E+008
'          E3:                    -1.955E+008
'       
'          e:                     -1.954891e+008
'          e0:                    -2e+008
'          e1:                    -2.0e+008
'          e2:                    -1.95e+008
'          e3:                    -1.955e+008
'       
'          F:                     -195489100.84
'          F0:                    -195489101
'          F1:                    -195489100.8
'          F2:                    -195489100.84
'          F3:                    -195489100.838
'       
'          G:                     -195489100.8377
'          G0:                    -195489100.8377
'          G1:                    -2E+08
'          G2:                    -2E+08
'          G3:                    -1.95E+08
'       
'          N:                     -195,489,100.84
'          N0:                    -195,489,101
'          N1:                    -195,489,100.8
'          N2:                    -195,489,100.84
'          N3:                    -195,489,100.838
'       
'          P:                     -19,548,910,083.77 %
'          P0:                    -19,548,910,084 %
'          P1:                    -19,548,910,083.8 %
'          P2:                    -19,548,910,083.77 %
'          P3:                    -19,548,910,083.770 %
'       
'          R:                     -195489100.8377
'          #,000.000:             -195,489,100.838
'          0.###E-000:            -1.955E008
'          000,000,000,000.00###: -000,195,489,100.8377
'       
'       Formatting of 1.0437E+21:
'          C:                     $1,043,700,000,000,000,000,000.00
'          C0:                    $1,043,700,000,000,000,000,000
'          C1:                    $1,043,700,000,000,000,000,000.0
'          C2:                    $1,043,700,000,000,000,000,000.00
'          C3:                    $1,043,700,000,000,000,000,000.000
'       
'          E:                     1.043700E+021
'          E0:                    1E+021
'          E1:                    1.0E+021
'          E2:                    1.04E+021
'          E3:                    1.044E+021
'       
'          e:                     1.043700e+021
'          e0:                    1e+021
'          e1:                    1.0e+021
'          e2:                    1.04e+021
'          e3:                    1.044e+021
'       
'          F:                     1043700000000000000000.00
'          F0:                    1043700000000000000000
'          F1:                    1043700000000000000000.0
'          F2:                    1043700000000000000000.00
'          F3:                    1043700000000000000000.000
'       
'          G:                     1.0437E+21
'          G0:                    1.0437E+21
'          G1:                    1E+21
'          G2:                    1E+21
'          G3:                    1.04E+21
'       
'          N:                     1,043,700,000,000,000,000,000.00
'          N0:                    1,043,700,000,000,000,000,000
'          N1:                    1,043,700,000,000,000,000,000.0
'          N2:                    1,043,700,000,000,000,000,000.00
'          N3:                    1,043,700,000,000,000,000,000.000
'       
'          P:                     104,370,000,000,000,000,000,000.00 %
'          P0:                    104,370,000,000,000,000,000,000 %
'          P1:                    104,370,000,000,000,000,000,000.0 %
'          P2:                    104,370,000,000,000,000,000,000.00 %
'          P3:                    104,370,000,000,000,000,000,000.000 %
'       
'          R:                     1.0437E+21
'          #,000.000:             1,043,700,000,000,000,000,000.000
'          0.###E-000:            1.044E021
'          000,000,000,000.00###: 1,043,700,000,000,000,000,000.00
'       
'       Formatting of -1.0573E-05:
'          C:                     $0.00
'          C0:                    $0
'          C1:                    $0.0
'          C2:                    $0.00
'          C3:                    $0.000
'       
'          E:                     -1.057300E-005
'          E0:                    -1E-005
'          E1:                    -1.1E-005
'          E2:                    -1.06E-005
'          E3:                    -1.057E-005
'       
'          e:                     -1.057300e-005
'          e0:                    -1e-005
'          e1:                    -1.1e-005
'          e2:                    -1.06e-005
'          e3:                    -1.057e-005
'       
'          F:                     0.00
'          F0:                    0
'          F1:                    0.0
'          F2:                    0.00
'          F3:                    0.000
'       
'          G:                     -1.0573E-05
'          G0:                    -1.0573E-05
'          G1:                    -1E-05
'          G2:                    -1.1E-05
'          G3:                    -1.06E-05
'       
'          N:                     0.00
'          N0:                    0
'          N1:                    0.0
'          N2:                    0.00
'          N3:                    0.000
'       
'          P:                     0.00 %
'          P0:                    0 %
'          P1:                    0.0 %
'          P2:                    0.00 %
'          P3:                    -0.001 %
'       
'          R:                     -1.0573E-05
'          #,000.000:             000.000
'          0.###E-000:            -1.057E-005
'          000,000,000,000.00###: -000,000,000,000.00001

설명

메서드 DoubleToString(String) 현재 문화권의 규칙을 사용하여 지정된 형식의 값 형식을 지정합니다. 다른 형식이나 문화권을 지정하려면 다음과 같이 메서드의 ToString 다른 오버로드를 사용합니다.

형식을 사용하려면 문화권의 경우 오버로드 사용
기본("G") 형식 기본(현재) 문화권 ToString()
기본("G") 형식 특정 문화권 ToString(IFormatProvider)
특정 형식 또는 전체 자릿수 특정 문화권 ToString(String, IFormatProvider)

반환 값은 에서 지정format한 대로 , NegativeInfinitySymbol, NaNSymbol또는 숫자의 문자열 표현일 수 있습니다PositiveInfinitySymbol.

매개 변수는 format 사용자 지정 숫자 형식 지정자의 조합뿐만 아니라 D 및 X를 제외한 유효한 표준 숫자 형식 지정자일 수 있습니다. 가 null 또는 빈 문자열인 경우 format 반환 값은 일반 숫자 형식 지정자("G")로 서식이 지정됩니다.

.NET은 다음과 같은 서식 지정 topics 자세히 설명하는 광범위한 서식 지정 지원을 제공합니다.

기본적으로 반환 값은 최대 17자리가 내부적으로 유지 관리되지만 15자리의 정밀도만 포함합니다. 이 instance 값이 15자리보다 크면 예상 숫자 대신 또는 NegativeInfinitySymbol 를 반환 PositiveInfinitySymbol 합니다ToString. 더 정밀도를 필요로 하는 경우 항상 17자리의 정밀도를 반환하는 "G17" 형식 사양 또는 숫자가 해당 정밀도로 표현될 수 있는 경우 15자리를 반환하는 "R" 또는 숫자가 최대 정밀도로만 표현될 수 있는 경우 17자리를 지정 format 합니다.

호출자 참고

/platform:x64 또는 /platform:anycpu 스위치를 사용하여 컴파일되고 64비트 시스템에서 실행되는 경우 "R" 표준 숫자 형식 문자열로 형식이 지정된 Double 값이 성공적으로 라운드트립되지 않는 경우가 있습니다. 이 문제를 해결하려면 "G17" 표준 숫자 형식 문자열을 사용하여 Double 값의 형식을 지정할 수 있습니다. 다음 예제에서는 성공적으로 라운드트립되지 않는 Double 값에 "R" 형식 문자열을 사용하고 "G17" 형식 문자열도 사용하여 원래 값을 성공적으로 라운드트립합니다.

using System;

public class Example
{
   static void Main(string[] args)
   {
      Console.WriteLine("Attempting to round-trip a Double with 'R':");
      double initialValue = 0.6822871999174;
      string valueString = initialValue.ToString("R");
      double roundTripped = double.Parse(valueString);
      Console.WriteLine("{0:R} = {1:R}: {2}\n",
                        initialValue, roundTripped, initialValue.Equals(roundTripped));

      Console.WriteLine("Attempting to round-trip a Double with 'G17':");
      string valueString17 = initialValue.ToString("G17");
      double roundTripped17 = double.Parse(valueString17);
      Console.WriteLine("{0:R} = {1:R}: {2}\n",
                        initialValue, roundTripped17, initialValue.Equals(roundTripped17));
   }
}
// If compiled to an application that targets anycpu or x64 and run on an x64 system,
// the example displays the following output:
//       Attempting to round-trip a Double with 'R':
//       0.6822871999174 = 0.68228719991740006: False
//
//       Attempting to round-trip a Double with 'G17':
//       0.6822871999174 = 0.6822871999174: True
open System

printfn "Attempting to round-trip a Double with 'R':"
let initialValue = 0.6822871999174
let valueString = initialValue.ToString "R"
let roundTripped = Double.Parse valueString
printfn $"{initialValue:R} = {roundTripped:R}: {initialValue.Equals roundTripped}\n"

printfn "Attempting to round-trip a Double with 'G17':"
let valueString17 = initialValue.ToString "G17"
let roundTripped17 = Double.Parse valueString17
printfn $"{initialValue:R} = {roundTripped17:R}: {initialValue.Equals roundTripped17}\n"
// If compiled to an application that targets anycpu or x64 and run on an x64 system,
// the example displays the following output:
//       Attempting to round-trip a Double with 'R':
//       0.6822871999174 = 0.68228719991740006: False
//
//       Attempting to round-trip a Double with 'G17':
//       0.6822871999174 = 0.6822871999174: True
Module Example
   Public Sub Main()
      Console.WriteLine("Attempting to round-trip a Double with 'R':")
      Dim initialValue As Double = 0.6822871999174
      Dim valueString As String = initialValue.ToString("R")
      Dim roundTripped As Double = Double.Parse(valueString)
      Console.WriteLine("{0:R} = {1:R}: {2}",
                        initialValue, roundTripped, initialValue.Equals(roundTripped))
      Console.WriteLine()

      Console.WriteLine("Attempting to round-trip a Double with 'G17':")
      Dim valueString17 As String = initialValue.ToString("G17")
      Dim roundTripped17 As Double = Double.Parse(valueString17)
      Console.WriteLine("{0:R} = {1:R}: {2}",
                        initialValue, roundTripped17, initialValue.Equals(roundTripped17))
   End Sub
End Module
' If compiled to an application that targets anycpu or x64 and run on an x64 system,
' the example displays the following output:
'       Attempting to round-trip a Double with 'R':
'       0.6822871999174 = 0.68228719991740006: False
'
'       Attempting to round-trip a Double with 'G17':
'       0.6822871999174 = 0.6822871999174: True

추가 정보

적용 대상

ToString(String, IFormatProvider)

지정된 형식 및 문화권별 형식 정보를 사용하여 이 인스턴스의 숫자 값을 해당 문자열 표현으로 변환합니다.

public:
 virtual System::String ^ ToString(System::String ^ format, IFormatProvider ^ provider);
public string ToString (string format, IFormatProvider provider);
public string ToString (string? format, IFormatProvider? provider);
override this.ToString : string * IFormatProvider -> string
Public Function ToString (format As String, provider As IFormatProvider) As String

매개 변수

format
String

숫자 서식 문자열입니다.

provider
IFormatProvider

문화권별 형식 정보를 제공하는 개체입니다.

반환

formatprovider로 지정된 이 인스턴스 값의 문자열 표현입니다.

구현

예제

다음 예제에서는 여러 문화권에 대해 지원되는 각 표준 숫자 형식 지정자를 사용하여 값을 표시 Double 합니다.

using namespace System;
using namespace System::Globalization;

int main(array<System::String ^> ^args)
{
   double value = 16325.62901;
   String^ specifier;
   CultureInfo^ culture;

   // Use standard numeric format specifiers.
   specifier = "G";
   culture = CultureInfo::CreateSpecificCulture("eu-ES");
   Console::WriteLine(value.ToString(specifier, culture));
   // Displays:    16325,62901
   Console::WriteLine(value.ToString(specifier, CultureInfo::InvariantCulture));
   // Displays:    16325.62901

   specifier = "C";
   culture = CultureInfo::CreateSpecificCulture("en-US");
   Console::WriteLine(value.ToString(specifier, culture));
   // Displays:    $16,325.63
   culture = CultureInfo::CreateSpecificCulture("en-GB");
   Console::WriteLine(value.ToString(specifier, culture));
   // Displays:    £16,325.63

   specifier = "E04";
   culture = CultureInfo::CreateSpecificCulture("sv-SE");
   Console::WriteLine(value.ToString(specifier, culture));
   // Displays: 1,6326E+004   
   culture = CultureInfo::CreateSpecificCulture("en-NZ");
   Console::WriteLine(value.ToString(specifier, culture));
   // Displays:    1.6326E+004   

   specifier = "F";
   culture = CultureInfo::CreateSpecificCulture("fr-FR");
   Console::WriteLine(value.ToString(specifier, culture));
   // Displays:    16325,63
   culture = CultureInfo::CreateSpecificCulture("en-CA");
   Console::WriteLine(value.ToString(specifier, culture));
   // Displays:    16325.63

   specifier = "N";
   culture = CultureInfo::CreateSpecificCulture("es-ES");
   Console::WriteLine(value.ToString(specifier, culture));
   // Displays:    16.325,63
   culture = CultureInfo::CreateSpecificCulture("fr-CA");
   Console::WriteLine(value.ToString(specifier, culture));
   // Displays:    16 325,63

   specifier = "P";
   culture = CultureInfo::InvariantCulture;
   Console::WriteLine((value/10000).ToString(specifier, culture));
   // Displays:    163.26 %
   culture = CultureInfo::CreateSpecificCulture("ar-EG");
   Console::WriteLine((value/10000).ToString(specifier, culture));
   // Displays:    163.256 %

   return 0;
}
double value = 16325.62901;
string specifier;
CultureInfo culture;

// Use standard numeric format specifiers.
specifier = "G";
culture = CultureInfo.CreateSpecificCulture("eu-ES");
Console.WriteLine(value.ToString(specifier, culture));
// Displays:    16325,62901
Console.WriteLine(value.ToString(specifier, CultureInfo.InvariantCulture));
// Displays:    16325.62901

specifier = "C";
culture = CultureInfo.CreateSpecificCulture("en-US");
Console.WriteLine(value.ToString(specifier, culture));
// Displays:    $16,325.63
culture = CultureInfo.CreateSpecificCulture("en-GB");
Console.WriteLine(value.ToString(specifier, culture));
// Displays:    £16,325.63

specifier = "E04";
culture = CultureInfo.CreateSpecificCulture("sv-SE");
Console.WriteLine(value.ToString(specifier, culture));
// Displays: 1,6326E+004
 culture = CultureInfo.CreateSpecificCulture("en-NZ");
 Console.WriteLine(value.ToString(specifier, culture));
// Displays:    1.6326E+004

specifier = "F";
culture = CultureInfo.CreateSpecificCulture("fr-FR");
Console.WriteLine(value.ToString(specifier, culture));
// Displays:    16325,63
culture = CultureInfo.CreateSpecificCulture("en-CA");
Console.WriteLine(value.ToString(specifier, culture));
// Displays:    16325.63

specifier = "N";
culture = CultureInfo.CreateSpecificCulture("es-ES");
Console.WriteLine(value.ToString(specifier, culture));
// Displays:    16.325,63
culture = CultureInfo.CreateSpecificCulture("fr-CA");
Console.WriteLine(value.ToString(specifier, culture));
// Displays:    16 325,63

specifier = "P";
culture = CultureInfo.InvariantCulture;
Console.WriteLine((value/10000).ToString(specifier, culture));
// Displays:    163.26 %
culture = CultureInfo.CreateSpecificCulture("ar-EG");
Console.WriteLine((value/10000).ToString(specifier, culture));
// Displays:    163.256 %
let value = 16325.62901

// Use standard numeric format specifiers.
let specifier = "G"
let culture = CultureInfo.CreateSpecificCulture "eu-ES"
printfn $"{value.ToString(specifier, culture)}"
// Displays:    16325,62901
printfn $"{value.ToString(specifier, CultureInfo.InvariantCulture)}"
// Displays:    16325.62901

let specifier = "C"
let culture = CultureInfo.CreateSpecificCulture "en-US"
printfn $"{value.ToString(specifier, culture)}"
// Displays:    $16,325.63
let culture = CultureInfo.CreateSpecificCulture "en-GB"
printfn $"{value.ToString(specifier, culture)}"
// Displays:    £16,325.63

let specifier = "E04"
let culture = CultureInfo.CreateSpecificCulture "sv-SE"
printfn $"{value.ToString(specifier, culture)}"
// Displays: 1,6326E+004
let culture = CultureInfo.CreateSpecificCulture "en-NZ"
printfn $"{value.ToString(specifier, culture)}"
// Displays:    1.6326E+004

let specifier = "F"
let culture = CultureInfo.CreateSpecificCulture "fr-FR"
printfn $"{value.ToString(specifier, culture)}"
// Displays:    16325,63
let culture = CultureInfo.CreateSpecificCulture "en-CA"
printfn $"{value.ToString(specifier, culture)}"
// Displays:    16325.63

let specifier = "N"
let culture = CultureInfo.CreateSpecificCulture "es-ES"
printfn $"{value.ToString(specifier, culture)}"
// Displays:    16.325,63
let culture = CultureInfo.CreateSpecificCulture "fr-CA"
printfn $"{value.ToString(specifier, culture)}"
// Displays:    16 325,63

let specifier = "P"
let culture = CultureInfo.InvariantCulture
printfn $"{(value / 10000.).ToString(specifier, culture)}"
// Displays:    163.26 %
let culture = CultureInfo.CreateSpecificCulture "ar-EG"
printfn $"{(value / 10000.).ToString(specifier, culture)}"
// Displays:    163.256 %
Dim value As Double = 16325.62901
Dim specifier As String
Dim culture As CultureInfo

' Use standard numeric format specifiers.
specifier = "G"
culture = CultureInfo.CreateSpecificCulture("eu-ES")
Console.WriteLine(value.ToString(specifier, culture))
' Displays:    16325,62901
Console.WriteLine(value.ToString(specifier, CultureInfo.InvariantCulture))
' Displays:    16325.62901

specifier = "C"
culture = CultureInfo.CreateSpecificCulture("en-US")
Console.WriteLine(value.ToString(specifier, culture))
' Displays:    $16,325.63
culture = CultureInfo.CreateSpecificCulture("en-GB")
Console.WriteLine(value.ToString(specifier, culture))
' Displays:    £16,325.63

specifier = "E04"
culture = CultureInfo.CreateSpecificCulture("sv-SE")
Console.WriteLine(value.ToString(specifier, culture))
' Displays: 1,6326E+004   
 culture = CultureInfo.CreateSpecificCulture("en-NZ")
 Console.WriteLine(value.ToString(specifier, culture))
' Displays:    1.6326E+004   

specifier = "F"
culture = CultureInfo.CreateSpecificCulture("fr-FR")
Console.WriteLine(value.ToString(specifier, culture))
' Displays:    16325,63
culture = CultureInfo.CreateSpecificCulture("en-CA")
Console.WriteLine(value.ToString(specifier, culture))
' Displays:    16325.63

specifier = "N"
culture = CultureInfo.CreateSpecificCulture("es-ES")
Console.WriteLine(value.ToString(specifier, culture))
' Displays:    16.325,63
culture = CultureInfo.CreateSpecificCulture("fr-CA")
Console.WriteLine(value.ToString(specifier, culture))
' Displays:    16 325,63

specifier = "P"
culture = CultureInfo.InvariantCulture
Console.WriteLine((value/10000).ToString(specifier, culture))
' Displays:    163.26 %
culture = CultureInfo.CreateSpecificCulture("ar-EG")
Console.WriteLine((value/10000).ToString(specifier, culture))
' Displays:    163.256 %

다음 예제에서는 및 를 매개 변수로 사용하여 StringIFormatProviderToString사용하는 것을 보여 줍니다.

public ref class Temperature: public IFormattable
{
   // IFormattable.ToString implementation.
public:
   virtual String^ ToString( String^ format, IFormatProvider^ provider )
   {
      if ( format != nullptr )
      {
         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 );
   }

protected:
   // The value holder
   double m_value;

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

   property double Celsius 
   {
      double get()
      {
         return (m_value - 32.0) / 1.8;
      }
      void set( double value )
      {
         m_value = 1.8 * value + 32.0;
      }
   }
};
public class Temperature : IFormattable {
    // IFormattable.ToString implementation.
    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;
        }
    }
}
type Temperature() =
    member val Value = 0. with get, set

    member this.Celsius
        with get () =
            (this.Value - 32.) / 1.8
        and set (value) =
            this.Value <- 1.8 * value + 32.

    // IFormattable.ToString implementation.
    interface IFormattable with
        // IFormattable.ToString implementation.
        member this.ToString(format: string, provider: IFormatProvider) =
            match format with
            | "F" ->
                $"{this.Value}'F"
            | "C" ->
                $"{this.Celsius}'C"
            | _ ->
                this.Value.ToString(format, provider)
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

설명

메서드는 ToString(String, IFormatProvider)Double 지정된 문화권의 지정된 형식으로 값의 형식을 지정합니다. 다른 형식이나 문화권을 지정하려면 다음과 같이 메서드의 ToString 다른 오버로드를 사용합니다.

형식을 사용하려면 문화권의 경우 오버로드 사용
기본("G") 형식 기본(현재) 문화권 ToString()
기본("G") 형식 특정 문화권 ToString(IFormatProvider)
특정 형식 또는 전체 자릿수 기본(현재) 문화권 ToString(String)

반환 값은 에서 지정format한 대로 , NegativeInfinitySymbol, NaNSymbol또는 숫자의 문자열 표현일 수 있습니다PositiveInfinitySymbol.

매개 변수는 format 사용자 지정 숫자 형식 지정자의 조합뿐만 아니라 D 및 X를 제외한 유효한 표준 숫자 형식 지정자일 수 있습니다. 가 null 또는 빈 문자열인 경우 format 이 instance 반환 값은 일반 숫자 형식 지정자("G")로 서식이 지정됩니다.

.NET은 다음과 같은 서식 지정 topics 자세히 설명하는 광범위한 서식 지정 지원을 제공합니다.

provider 매개 변수는 메서드가 개체를 IFormatProvider 반환하는 GetFormat 구현입니다NumberFormatInfo. 일반적으로 는 providerCultureInfo 개체 또는 개체입니다 NumberFormatInfo . 매개 변수는 provider 서식 지정에 사용되는 문화권별 정보를 제공합니다. 가 이nullprovider 반환 값은 현재 문화권에 대한 개체를 NumberFormatInfo 사용하여 형식이 지정됩니다.

기본적으로 반환 값은 최대 17자리가 내부적으로 유지 관리되지만 15자리의 정밀도만 포함합니다. 이 instance 값이 15자리보다 크면 예상 숫자 대신 또는 NegativeInfinitySymbol 를 반환 PositiveInfinitySymbol 합니다ToString. 더 정밀도를 필요로 하는 경우 항상 17자리의 정밀도를 반환하는 "G17" 형식 사양 또는 숫자가 해당 정밀도로 표현될 수 있는 경우 15자리를 반환하는 "R" 또는 숫자가 최대 정밀도로만 표현될 수 있는 경우 17자리를 지정 format 합니다.

호출자 참고

/platform:x64 또는 /platform:anycpu 스위치를 사용하여 컴파일되고 64비트 시스템에서 실행되는 경우 "R" 표준 숫자 형식 문자열로 형식이 지정된 Double 값이 성공적으로 라운드트립되지 않는 경우가 있습니다. 이 문제를 해결하려면 "G17" 표준 숫자 형식 문자열을 사용하여 Double 값의 형식을 지정할 수 있습니다. 다음 예제에서는 성공적으로 라운드트립되지 않는 Double 값에 "R" 형식 문자열을 사용하고 "G17" 형식 문자열도 사용하여 원래 값을 성공적으로 라운드트립합니다.

using System;
using System.Globalization;

public class Example
{
   static void Main(string[] args)
   {
      Console.WriteLine("Attempting to round-trip a Double with 'R':");
      double initialValue = 0.6822871999174;
      string valueString = initialValue.ToString("R",
                                                 CultureInfo.InvariantCulture);
      double roundTripped = double.Parse(valueString,
                                         CultureInfo.InvariantCulture);
      Console.WriteLine("{0:R} = {1:R}: {2}\n",
                        initialValue, roundTripped, initialValue.Equals(roundTripped));

      Console.WriteLine("Attempting to round-trip a Double with 'G17':");
      string valueString17 = initialValue.ToString("G17",
                                                   CultureInfo.InvariantCulture);
      double roundTripped17 = double.Parse(valueString17,
                                           CultureInfo.InvariantCulture);
      Console.WriteLine("{0:R} = {1:R}: {2}\n",
                        initialValue, roundTripped17, initialValue.Equals(roundTripped17));
      // If compiled to an application that targets anycpu or x64 and run on an x64 system,
      // the example displays the following output:
      //       Attempting to round-trip a Double with 'R':
      //       0.6822871999174 = 0.68228719991740006: False
      //
      //       Attempting to round-trip a Double with 'G17':
      //       0.6822871999174 = 0.6822871999174: True
   }
}
open System
open System.Globalization

printfn "Attempting to round-trip a Double with 'R':"
let initialValue = 0.6822871999174
let valueString = initialValue.ToString("R", CultureInfo.InvariantCulture)
let roundTripped = Double.Parse(valueString, CultureInfo.InvariantCulture)
printfn $"{initialValue:R} = {roundTripped:R}: {initialValue.Equals roundTripped}\n"

printfn "Attempting to round-trip a Double with 'G17':"
let valueString17 = initialValue.ToString("G17", CultureInfo.InvariantCulture)
let roundTripped17 = Double.Parse(valueString17, CultureInfo.InvariantCulture)
printfn $"{initialValue:R} = {roundTripped17:R}: {initialValue.Equals roundTripped17}\n"
// If compiled to an application that targets anycpu or x64 and run on an x64 system,
// the example displays the following output:
//       Attempting to round-trip a Double with 'R':
//       0.6822871999174 = 0.68228719991740006: False
//
//       Attempting to round-trip a Double with 'G17':
//       0.6822871999174 = 0.6822871999174: True
Imports System.Globalization

Module Example
   Public Sub Main()
      Console.WriteLine("Attempting to round-trip a Double with 'R':")
      Dim initialValue As Double = 0.6822871999174
      Dim valueString As String = initialValue.ToString("R",
                                               CultureInfo.InvariantCulture)
      Dim roundTripped As Double = Double.Parse(valueString,
                                                CultureInfo.InvariantCulture)
      Console.WriteLine("{0:R} = {1:R}: {2}",
                        initialValue, roundTripped, initialValue.Equals(roundTripped))
      Console.WriteLine()
      
      Console.WriteLine("Attempting to round-trip a Double with 'G17':")
      Dim valueString17 As String = initialValue.ToString("G17",
                                                 CultureInfo.InvariantCulture)
      Dim roundTripped17 As Double = double.Parse(valueString17,
                                            CultureInfo.InvariantCulture)
      Console.WriteLine("{0:R} = {1:R}: {2}",
                        initialValue, roundTripped17, initialValue.Equals(roundTripped17))
   End Sub
End Module
' If compiled to an application that targets anycpu or x64 and run on an x64 system,
' the example displays the following output:
'       Attempting to round-trip a Double with 'R':
'       0.6822871999174 = 0.68228719991740006: False
'
'       Attempting to round-trip a Double with 'G17':
'       0.6822871999174 = 0.6822871999174: True

추가 정보

적용 대상