다음을 통해 공유


Double 구조체

배정밀도 부동 소수점 숫자를 나타냅니다.

네임스페이스: System
어셈블리: mscorlib(mscorlib.dll)

구문

‘선언
<SerializableAttribute> _
<ComVisibleAttribute(True)> _
Public Structure Double
    Implements IComparable, IFormattable, IConvertible, IComparable(Of Double), _
    IEquatable(Of Double)
‘사용 방법
Dim instance As Double
[SerializableAttribute] 
[ComVisibleAttribute(true)] 
public struct Double : IComparable, IFormattable, IConvertible, 
    IComparable<double>, IEquatable<double>
[SerializableAttribute] 
[ComVisibleAttribute(true)] 
public value class Double : IComparable, IFormattable, IConvertible, 
    IComparable<double>, IEquatable<double>
/** @attribute SerializableAttribute() */ 
/** @attribute ComVisibleAttribute(true) */ 
public final class Double extends ValueType implements IComparable, IFormattable, 
    IConvertible, IComparable<double>, IEquatable<double>
JScript에서는 구조체를 사용할 수 있지만 새로 선언할 수는 없습니다.

설명

Double 값 형식은 배정밀도 64비트 숫자를 나타내며, 음수 1.79769313486232e308과 양수 1.79769313486232e308 사이의 값을 가지고, 양수 또는 음수 0, PositiveInfinity, NegativeInfinity 및 Not-a-Number(NaN)도 포함됩니다.

Double은 이진 부동 소수점 산술 연산에 대한 IEC 60559:1989(IEEE 754) 표준을 따릅니다.

Double은 이 형식의 인스턴스를 비교하고 인스턴스의 값을 문자열 표현으로 변환하며 숫자의 문자열 표현을 이 형식의 인스턴스로 변환하는 메서드를 제공합니다. 형식 사양 코드로 값 형식의 문자열 표현을 제어하는 방법에 대한 자세한 내용은 형식 지정 개요, 표준 숫자 형식 문자열사용자 지정 숫자 형식 문자열을 참조하십시오.

부동 소수점 숫자 사용

이진 연산을 수행할 때 피연산자 중 하나가 Double이면 다른 피연산자는 정수 계열 형식이거나 부동 소수점 형식(Double 또는 Single)이어야 합니다. 다른 피연산자가 Double이 아니면 연산을 수행하기 전에 Double로 변환되고 최소의 Double 범위와 정밀도를 사용하여 연산이 수행됩니다. 연산 결과가 숫자이면 결과 형식은 Double입니다.

대입 연산자를 포함하여 부동 소수점 연산자는 예외를 throw하지 않습니다. 대신 예외 상황이 발생하면 부동 소수점 연산의 결과는 아래 설명에서처럼 0, 무한대 또는 NaN입니다.

  • 부동 소수점 연산의 결과가 대상 형식에 비해 너무 작으면 연산 결과는 0입니다.

  • 부동 소수점 연산의 결과가 대상 형식에 비해 너무 크면 연산 결과는 결과의 부호에 따라 PositiveInfinity 또는 NegativeInfinity입니다.

  • 부동 소수점 연산이 잘못되었으면 연산 결과는 NaN입니다.

  • 부동 소수점 연산의 피연산자 중 하나 또는 모두가 NaN이면 연산 결과는 NaN입니다.

부동 소수점 숫자는 10진수를 대략적으로만 나타낼 수 있으며 부동 소수점 숫자의 전체 자릿수에 따라 10진수의 근사값인 해당 값의 정밀도가 달라집니다. 내부적으로는 최대 17자리의 정밀도를 유지 관리하지만 Double 값은 기본적으로 15자리를 포함합니다. 부동 소수점 숫자의 자릿수에 따라 다음과 같은 여러 경우가 발생합니다.

  • 특정 정밀도에 대해 동일하게 나타나는 두 개의 부동 소수점 숫자는 최소 유효 자릿수가 다르기 때문에 서로 다른 것으로 간주될 수 있습니다.

  • 부동 소수점 숫자를 사용하는 수학 연산이나 비교 연산에서는 부동 소수점 숫자로 10진수를 정확하게 나타낼 수 없기 때문에 10진수가 사용되는 경우 동일한 결과를 생성하지 않을 수 있습니다.

  • 부동 소수점 숫자가 포함된 경우 값에 라운드트립이 발생하지 않을 수 있습니다. 연산에서 원래 부동 소수점 숫자를 다른 형식으로 변환하고, 역 연산에서 변환된 형식을 다시 부동 소수점 숫자로 변환하며, 최종 부동 소수점 숫자가 원래 부동 소수점 숫자와 같으면 해당 값을 라운드트립 값이라고 합니다. 변환할 때 하나 이상의 최소 유효 자릿수가 손실되거나 변경되기 때문에 라운드트립이 실패할 수 있습니다.

인터페이스 구현

이 형식은 IComparable, IComparable, IFormattableIConvertible 인터페이스를 구현합니다. 변환을 위해 이 형식의 IConvertible 명시적 인터페이스 멤버 구현 대신 Convert 클래스를 사용합니다.

예제

다음 코드 예제에서는 Double를 사용합니다.

' Temperature class stores the value as Double
' and delegates most of the functionality 
' to the Double implementation.
Public Class Temperature
    Implements IComparable, IFormattable

    Public Overloads Function CompareTo(ByVal obj As Object) As Integer _
        Implements IComparable.CompareTo

        If TypeOf obj Is Temperature Then
            Dim temp As Temperature = CType(obj, Temperature)

            Return m_value.CompareTo(temp.m_value)
        End If

        Throw New ArgumentException("object is not a Temperature")
    End Function

    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

    ' Parses the temperature from a string in form
    ' [ws][sign]digits['F|'C][ws]
    Public Shared Function Parse(ByVal s As String, ByVal styles As NumberStyles, ByVal provider As IFormatProvider) As Temperature
        Dim temp As New Temperature()

        If s.TrimEnd(Nothing).EndsWith("'F") Then
            temp.Value = Double.Parse(s.Remove(s.LastIndexOf("'"c), 2), styles, provider)
        Else
            If s.TrimEnd(Nothing).EndsWith("'C") Then
                temp.Celsius = Double.Parse(s.Remove(s.LastIndexOf("'"c), 2), styles, provider)
            Else
                temp.Value = Double.Parse(s, styles, provider)
            End If
        End If
        Return temp
    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
/// <summary>
/// Temperature class stores the value as Double
/// and delegates most of the functionality 
/// to the Double implementation.
/// </summary>
public class Temperature : IComparable, IFormattable {
    /// <summary>
    /// IComparable.CompareTo implementation.
    /// </summary>
    public int CompareTo(object obj) {
        if(obj is Temperature) {
            Temperature temp = (Temperature) obj;

            return m_value.CompareTo(temp.m_value);
        }
        
        throw new ArgumentException("object is not a Temperature");    
    }

    /// <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);
    }

    /// <summary>
    /// Parses the temperature from a string in form
    /// [ws][sign]digits['F|'C][ws]
    /// </summary>
    public static Temperature Parse(string s, NumberStyles styles, IFormatProvider provider) {
        Temperature temp = new Temperature();

        if( s.TrimEnd(null).EndsWith("'F") ) {
            temp.Value = Double.Parse( s.Remove(s.LastIndexOf('\''), 2), styles, provider);
        }
        else if( s.TrimEnd(null).EndsWith("'C") ) {
            temp.Celsius = Double.Parse( s.Remove(s.LastIndexOf('\''), 2), styles, provider);
        }
        else {
            temp.Value = Double.Parse(s, styles, provider);
        }

        return temp;
    }

    // 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;
        }
    }
}
/// <summary>
/// Temperature class stores the value as Double
/// and delegates most of the functionality 
/// to the Double implementation.
/// </summary>
public ref class Temperature: public IComparable, public IFormattable
{
   /// <summary>
   /// IComparable.CompareTo implementation.
   /// </summary>
public:
   virtual int CompareTo( Object^ obj )
   {
      if ( dynamic_cast<Temperature^>(obj) )
      {
         Temperature^ temp = (Temperature^)(obj);
         return m_value.CompareTo( temp->m_value );
      }

      throw gcnew ArgumentException( "object is not a Temperature" );
   }

   /// <summary>
   /// IFormattable.ToString implementation.
   /// </summary>
   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 );
   }


   /// <summary>
   /// Parses the temperature from a string in form
   /// [ws][sign]digits['F|'C][ws]
   /// </summary>
   static Temperature^ Parse( String^ s, NumberStyles styles, IFormatProvider^ provider )
   {
      Temperature^ temp = gcnew Temperature;

      if ( s->TrimEnd( 0 )->EndsWith( "'F" ) )
      {
         temp->Value = Double::Parse( s->Remove( s->LastIndexOf( '\'' ), 2 ), styles, provider );
      }
      else
      if ( s->TrimEnd( 0 )->EndsWith( "'C" ) )
      {
         temp->Celsius = Double::Parse( s->Remove( s->LastIndexOf( '\'' ), 2 ), styles, provider );
      }
      else
      {
         temp->Value = Double::Parse( s, styles, provider );
      }

      return temp;
   }

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;
      }

   }

};
/// <summary>
/// Temperature class stores the value as Double
/// and delegates most of the functionality 
/// to the Double implementation.
/// </summary>
public class Temperature implements IComparable, IFormattable {
    /// <summary>
    /// IComparable.CompareTo implementation.
    /// </summary>
    public function CompareTo(obj) : int{
        if(obj.GetType() == Temperature) {
            var temp : Temperature = Temperature(obj);

            return m_value.CompareTo(temp.m_value);
        }
        
        throw new ArgumentException("object is not a Temperature");    
    }

    /// <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);
    }

    /// <summary>
    /// Parses the temperature from a string in form
    /// [ws][sign]digits['F|'C][ws]
    /// </summary>
    public static function Parse(s : String, styles : NumberStyles, provider : IFormatProvider) : Temperature{
        var temp : Temperature = new Temperature();

        if( s.TrimEnd(null).EndsWith("'F") ) {
            temp.Value = Double.Parse( s.Remove(s.LastIndexOf('\''), 2), styles, provider);
        }
        else if( s.TrimEnd(null).EndsWith("'C") ) {
            temp.Celsius = Double.Parse( s.Remove(s.LastIndexOf('\''), 2), styles, provider);
        }
        else {
            temp.Value = Double.Parse(s, styles, provider);
        }

        return temp;
    }

    // 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;
    }
}

스레드로부터의 안전성

이 형식의 모든 public static(Visual Basic의 경우 Shared) 멤버는 스레드로부터 안전합니다. 인터페이스 멤버는 스레드로부터 안전하지 않습니다.

플랫폼

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

.NET Framework에서 모든 플래폼의 모든 버전을 지원하지는 않습니다. 지원되는 버전의 목록은 시스템 요구 사항을 참조하십시오.

버전 정보

.NET Framework

2.0, 1.1, 1.0에서 지원

.NET Compact Framework

2.0, 1.0에서 지원

참고 항목

참조

Double 멤버
System 네임스페이스