Double 结构

定义

表示双精度浮点数。

public value class double : IComparable, IComparable<double>, IConvertible, IEquatable<double>, IFormattable
public value class double : IComparable<double>, IConvertible, IEquatable<double>, IParsable<double>, ISpanParsable<double>, IUtf8SpanParsable<double>, System::Numerics::IAdditionOperators<double, double, double>, System::Numerics::IAdditiveIdentity<double, double>, System::Numerics::IBinaryFloatingPointIeee754<double>, System::Numerics::IBinaryNumber<double>, System::Numerics::IBitwiseOperators<double, double, double>, System::Numerics::IComparisonOperators<double, double, bool>, System::Numerics::IDecrementOperators<double>, System::Numerics::IDivisionOperators<double, double, double>, System::Numerics::IEqualityOperators<double, double, bool>, System::Numerics::IExponentialFunctions<double>, System::Numerics::IFloatingPoint<double>, System::Numerics::IFloatingPointConstants<double>, System::Numerics::IFloatingPointIeee754<double>, System::Numerics::IHyperbolicFunctions<double>, System::Numerics::IIncrementOperators<double>, System::Numerics::ILogarithmicFunctions<double>, System::Numerics::IMinMaxValue<double>, System::Numerics::IModulusOperators<double, double, double>, System::Numerics::IMultiplicativeIdentity<double, double>, System::Numerics::IMultiplyOperators<double, double, double>, System::Numerics::INumber<double>, System::Numerics::INumberBase<double>, System::Numerics::IPowerFunctions<double>, System::Numerics::IRootFunctions<double>, System::Numerics::ISignedNumber<double>, System::Numerics::ISubtractionOperators<double, double, double>, System::Numerics::ITrigonometricFunctions<double>, System::Numerics::IUnaryNegationOperators<double, double>, System::Numerics::IUnaryPlusOperators<double, double>
public value class double : IComparable, IComparable<double>, IConvertible, IEquatable<double>, ISpanFormattable
public value class double : IComparable<double>, IConvertible, IEquatable<double>, IParsable<double>, ISpanParsable<double>, System::Numerics::IAdditionOperators<double, double, double>, System::Numerics::IAdditiveIdentity<double, double>, System::Numerics::IBinaryFloatingPointIeee754<double>, System::Numerics::IBinaryNumber<double>, System::Numerics::IBitwiseOperators<double, double, double>, System::Numerics::IComparisonOperators<double, double, bool>, System::Numerics::IDecrementOperators<double>, System::Numerics::IDivisionOperators<double, double, double>, System::Numerics::IEqualityOperators<double, double, bool>, System::Numerics::IExponentialFunctions<double>, System::Numerics::IFloatingPoint<double>, System::Numerics::IFloatingPointConstants<double>, System::Numerics::IFloatingPointIeee754<double>, System::Numerics::IHyperbolicFunctions<double>, System::Numerics::IIncrementOperators<double>, System::Numerics::ILogarithmicFunctions<double>, System::Numerics::IMinMaxValue<double>, System::Numerics::IModulusOperators<double, double, double>, System::Numerics::IMultiplicativeIdentity<double, double>, System::Numerics::IMultiplyOperators<double, double, double>, System::Numerics::INumber<double>, System::Numerics::INumberBase<double>, System::Numerics::IPowerFunctions<double>, System::Numerics::IRootFunctions<double>, System::Numerics::ISignedNumber<double>, System::Numerics::ISubtractionOperators<double, double, double>, System::Numerics::ITrigonometricFunctions<double>, System::Numerics::IUnaryNegationOperators<double, double>, System::Numerics::IUnaryPlusOperators<double, double>
public value class double : IComparable, IConvertible, IFormattable
public value class double : IComparable, IComparable<double>, IEquatable<double>, IFormattable
public struct Double : IComparable, IComparable<double>, IConvertible, IEquatable<double>, IFormattable
public readonly struct Double : IComparable<double>, IConvertible, IEquatable<double>, IParsable<double>, ISpanParsable<double>, IUtf8SpanParsable<double>, System.Numerics.IAdditionOperators<double,double,double>, System.Numerics.IAdditiveIdentity<double,double>, System.Numerics.IBinaryFloatingPointIeee754<double>, System.Numerics.IBinaryNumber<double>, System.Numerics.IBitwiseOperators<double,double,double>, System.Numerics.IComparisonOperators<double,double,bool>, System.Numerics.IDecrementOperators<double>, System.Numerics.IDivisionOperators<double,double,double>, System.Numerics.IEqualityOperators<double,double,bool>, System.Numerics.IExponentialFunctions<double>, System.Numerics.IFloatingPoint<double>, System.Numerics.IFloatingPointConstants<double>, System.Numerics.IFloatingPointIeee754<double>, System.Numerics.IHyperbolicFunctions<double>, System.Numerics.IIncrementOperators<double>, System.Numerics.ILogarithmicFunctions<double>, System.Numerics.IMinMaxValue<double>, System.Numerics.IModulusOperators<double,double,double>, System.Numerics.IMultiplicativeIdentity<double,double>, System.Numerics.IMultiplyOperators<double,double,double>, System.Numerics.INumber<double>, System.Numerics.INumberBase<double>, System.Numerics.IPowerFunctions<double>, System.Numerics.IRootFunctions<double>, System.Numerics.ISignedNumber<double>, System.Numerics.ISubtractionOperators<double,double,double>, System.Numerics.ITrigonometricFunctions<double>, System.Numerics.IUnaryNegationOperators<double,double>, System.Numerics.IUnaryPlusOperators<double,double>
public readonly struct Double : IComparable, IComparable<double>, IConvertible, IEquatable<double>, IFormattable
public readonly struct Double : IComparable, IComparable<double>, IConvertible, IEquatable<double>, ISpanFormattable
public readonly struct Double : IComparable<double>, IConvertible, IEquatable<double>, IParsable<double>, ISpanParsable<double>, System.Numerics.IAdditionOperators<double,double,double>, System.Numerics.IAdditiveIdentity<double,double>, System.Numerics.IBinaryFloatingPointIeee754<double>, System.Numerics.IBinaryNumber<double>, System.Numerics.IBitwiseOperators<double,double,double>, System.Numerics.IComparisonOperators<double,double,bool>, System.Numerics.IDecrementOperators<double>, System.Numerics.IDivisionOperators<double,double,double>, System.Numerics.IEqualityOperators<double,double,bool>, System.Numerics.IExponentialFunctions<double>, System.Numerics.IFloatingPoint<double>, System.Numerics.IFloatingPointConstants<double>, System.Numerics.IFloatingPointIeee754<double>, System.Numerics.IHyperbolicFunctions<double>, System.Numerics.IIncrementOperators<double>, System.Numerics.ILogarithmicFunctions<double>, System.Numerics.IMinMaxValue<double>, System.Numerics.IModulusOperators<double,double,double>, System.Numerics.IMultiplicativeIdentity<double,double>, System.Numerics.IMultiplyOperators<double,double,double>, System.Numerics.INumber<double>, System.Numerics.INumberBase<double>, System.Numerics.IPowerFunctions<double>, System.Numerics.IRootFunctions<double>, System.Numerics.ISignedNumber<double>, System.Numerics.ISubtractionOperators<double,double,double>, System.Numerics.ITrigonometricFunctions<double>, System.Numerics.IUnaryNegationOperators<double,double>, System.Numerics.IUnaryPlusOperators<double,double>
[System.Serializable]
public struct Double : IComparable, IConvertible, IFormattable
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public struct Double : IComparable, IComparable<double>, IConvertible, IEquatable<double>, IFormattable
public struct Double : IComparable, IComparable<double>, IEquatable<double>, IFormattable
type double = struct
    interface IConvertible
    interface IFormattable
type double = struct
    interface IConvertible
    interface IFormattable
    interface IParsable<double>
    interface ISpanFormattable
    interface ISpanParsable<double>
    interface IUtf8SpanFormattable
    interface IUtf8SpanParsable<double>
    interface IAdditionOperators<double, double, double>
    interface IAdditiveIdentity<double, double>
    interface IBinaryFloatingPointIeee754<double>
    interface IBinaryNumber<double>
    interface IBitwiseOperators<double, double, double>
    interface IComparisonOperators<double, double, bool>
    interface IEqualityOperators<double, double, bool>
    interface IDecrementOperators<double>
    interface IDivisionOperators<double, double, double>
    interface IIncrementOperators<double>
    interface IModulusOperators<double, double, double>
    interface IMultiplicativeIdentity<double, double>
    interface IMultiplyOperators<double, double, double>
    interface INumber<double>
    interface INumberBase<double>
    interface ISubtractionOperators<double, double, double>
    interface IUnaryNegationOperators<double, double>
    interface IUnaryPlusOperators<double, double>
    interface IExponentialFunctions<double>
    interface IFloatingPointConstants<double>
    interface IFloatingPoint<double>
    interface ISignedNumber<double>
    interface IFloatingPointIeee754<double>
    interface IHyperbolicFunctions<double>
    interface ILogarithmicFunctions<double>
    interface IPowerFunctions<double>
    interface IRootFunctions<double>
    interface ITrigonometricFunctions<double>
    interface IMinMaxValue<double>
type double = struct
    interface IConvertible
    interface ISpanFormattable
    interface IFormattable
type double = struct
    interface IConvertible
    interface IFormattable
    interface IParsable<double>
    interface ISpanFormattable
    interface ISpanParsable<double>
    interface IAdditionOperators<double, double, double>
    interface IAdditiveIdentity<double, double>
    interface IBinaryFloatingPointIeee754<double>
    interface IBinaryNumber<double>
    interface IBitwiseOperators<double, double, double>
    interface IComparisonOperators<double, double, bool>
    interface IEqualityOperators<double, double, bool>
    interface IDecrementOperators<double>
    interface IDivisionOperators<double, double, double>
    interface IIncrementOperators<double>
    interface IModulusOperators<double, double, double>
    interface IMultiplicativeIdentity<double, double>
    interface IMultiplyOperators<double, double, double>
    interface INumber<double>
    interface INumberBase<double>
    interface ISubtractionOperators<double, double, double>
    interface IUnaryNegationOperators<double, double>
    interface IUnaryPlusOperators<double, double>
    interface IExponentialFunctions<double>
    interface IFloatingPointConstants<double>
    interface IFloatingPoint<double>
    interface ISignedNumber<double>
    interface IFloatingPointIeee754<double>
    interface IHyperbolicFunctions<double>
    interface ILogarithmicFunctions<double>
    interface IPowerFunctions<double>
    interface IRootFunctions<double>
    interface ITrigonometricFunctions<double>
    interface IMinMaxValue<double>
type double = struct
    interface IConvertible
    interface IFormattable
    interface IParsable<double>
    interface ISpanFormattable
    interface ISpanParsable<double>
    interface IAdditionOperators<double, double, double>
    interface IAdditiveIdentity<double, double>
    interface IBinaryFloatingPointIeee754<double>
    interface IBinaryNumber<double>
    interface IBitwiseOperators<double, double, double>
    interface IComparisonOperators<double, double, bool>
    interface IEqualityOperators<double, double, bool>
    interface IDecrementOperators<double>
    interface IDivisionOperators<double, double, double>
    interface IIncrementOperators<double>
    interface IModulusOperators<double, double, double>
    interface IMultiplicativeIdentity<double, double>
    interface IMultiplyOperators<double, double, double>
    interface INumber<double>
    interface INumberBase<double>
    interface ISubtractionOperators<double, double, double>
    interface IUnaryNegationOperators<double, double>
    interface IUnaryPlusOperators<double, double>
    interface IUtf8SpanFormattable
    interface IUtf8SpanParsable<double>
    interface IExponentialFunctions<double>
    interface IFloatingPointConstants<double>
    interface IFloatingPoint<double>
    interface ISignedNumber<double>
    interface IFloatingPointIeee754<double>
    interface IHyperbolicFunctions<double>
    interface ILogarithmicFunctions<double>
    interface IPowerFunctions<double>
    interface IRootFunctions<double>
    interface ITrigonometricFunctions<double>
    interface IMinMaxValue<double>
[<System.Serializable>]
type double = struct
    interface IFormattable
    interface IConvertible
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type double = struct
    interface IFormattable
    interface IConvertible
type double = struct
    interface IFormattable
Public Structure Double
Implements IComparable, IComparable(Of Double), IConvertible, IEquatable(Of Double), IFormattable
Public Structure Double
Implements IAdditionOperators(Of Double, Double, Double), IAdditiveIdentity(Of Double, Double), IBinaryFloatingPointIeee754(Of Double), IBinaryNumber(Of Double), IBitwiseOperators(Of Double, Double, Double), IComparable(Of Double), IComparisonOperators(Of Double, Double, Boolean), IConvertible, IDecrementOperators(Of Double), IDivisionOperators(Of Double, Double, Double), IEqualityOperators(Of Double, Double, Boolean), IEquatable(Of Double), IExponentialFunctions(Of Double), IFloatingPoint(Of Double), IFloatingPointConstants(Of Double), IFloatingPointIeee754(Of Double), IHyperbolicFunctions(Of Double), IIncrementOperators(Of Double), ILogarithmicFunctions(Of Double), IMinMaxValue(Of Double), IModulusOperators(Of Double, Double, Double), IMultiplicativeIdentity(Of Double, Double), IMultiplyOperators(Of Double, Double, Double), INumber(Of Double), INumberBase(Of Double), IParsable(Of Double), IPowerFunctions(Of Double), IRootFunctions(Of Double), ISignedNumber(Of Double), ISpanParsable(Of Double), ISubtractionOperators(Of Double, Double, Double), ITrigonometricFunctions(Of Double), IUnaryNegationOperators(Of Double, Double), IUnaryPlusOperators(Of Double, Double), IUtf8SpanParsable(Of Double)
Public Structure Double
Implements IComparable, IComparable(Of Double), IConvertible, IEquatable(Of Double), ISpanFormattable
Public Structure Double
Implements IAdditionOperators(Of Double, Double, Double), IAdditiveIdentity(Of Double, Double), IBinaryFloatingPointIeee754(Of Double), IBinaryNumber(Of Double), IBitwiseOperators(Of Double, Double, Double), IComparable(Of Double), IComparisonOperators(Of Double, Double, Boolean), IConvertible, IDecrementOperators(Of Double), IDivisionOperators(Of Double, Double, Double), IEqualityOperators(Of Double, Double, Boolean), IEquatable(Of Double), IExponentialFunctions(Of Double), IFloatingPoint(Of Double), IFloatingPointConstants(Of Double), IFloatingPointIeee754(Of Double), IHyperbolicFunctions(Of Double), IIncrementOperators(Of Double), ILogarithmicFunctions(Of Double), IMinMaxValue(Of Double), IModulusOperators(Of Double, Double, Double), IMultiplicativeIdentity(Of Double, Double), IMultiplyOperators(Of Double, Double, Double), INumber(Of Double), INumberBase(Of Double), IParsable(Of Double), IPowerFunctions(Of Double), IRootFunctions(Of Double), ISignedNumber(Of Double), ISpanParsable(Of Double), ISubtractionOperators(Of Double, Double, Double), ITrigonometricFunctions(Of Double), IUnaryNegationOperators(Of Double, Double), IUnaryPlusOperators(Of Double, Double)
Public Structure Double
Implements IComparable, IConvertible, IFormattable
Public Structure Double
Implements IComparable, IComparable(Of Double), IEquatable(Of Double), IFormattable
继承
Double
属性
实现
IComparable IComparable<Double> IConvertible IEquatable<Double> IFormattable IComparable<TSelf> IEquatable<TSelf> IParsable<Double> IParsable<TSelf> ISpanFormattable ISpanParsable<Double> ISpanParsable<TSelf> IUtf8SpanFormattable IUtf8SpanParsable<Double> IUtf8SpanParsable<TSelf> IAdditionOperators<Double,Double,Double> IAdditionOperators<TSelf,TSelf,TSelf> IAdditiveIdentity<Double,Double> IAdditiveIdentity<TSelf,TSelf> IBinaryFloatingPointIeee754<Double> IBinaryNumber<Double> IBinaryNumber<TSelf> IBitwiseOperators<Double,Double,Double> IBitwiseOperators<TSelf,TSelf,TSelf> IComparisonOperators<Double,Double,Boolean> IComparisonOperators<TSelf,TSelf,Boolean> IDecrementOperators<Double> IDecrementOperators<TSelf> IDivisionOperators<Double,Double,Double> IDivisionOperators<TSelf,TSelf,TSelf> IEqualityOperators<Double,Double,Boolean> IEqualityOperators<TSelf,TOther,TResult> IEqualityOperators<TSelf,TSelf,Boolean> IExponentialFunctions<Double> IExponentialFunctions<TSelf> IFloatingPoint<Double> IFloatingPoint<TSelf> IFloatingPointConstants<Double> IFloatingPointConstants<TSelf> IFloatingPointIeee754<Double> IFloatingPointIeee754<TSelf> IHyperbolicFunctions<Double> IHyperbolicFunctions<TSelf> IIncrementOperators<Double> IIncrementOperators<TSelf> ILogarithmicFunctions<Double> ILogarithmicFunctions<TSelf> IMinMaxValue<Double> IModulusOperators<Double,Double,Double> IModulusOperators<TSelf,TSelf,TSelf> IMultiplicativeIdentity<Double,Double> IMultiplicativeIdentity<TSelf,TSelf> IMultiplyOperators<Double,Double,Double> IMultiplyOperators<TSelf,TSelf,TSelf> INumber<Double> INumber<TSelf> INumberBase<Double> INumberBase<TSelf> IPowerFunctions<Double> IPowerFunctions<TSelf> IRootFunctions<Double> IRootFunctions<TSelf> ISignedNumber<Double> ISignedNumber<TSelf> ISubtractionOperators<Double,Double,Double> ISubtractionOperators<TSelf,TSelf,TSelf> ITrigonometricFunctions<Double> ITrigonometricFunctions<TSelf> IUnaryNegationOperators<Double,Double> IUnaryNegationOperators<TSelf,TSelf> IUnaryPlusOperators<Double,Double> IUnaryPlusOperators<TSelf,TSelf>

示例

以下代码示例展示了 Double 的用法:

// The Temperature class stores the temperature as a Double
// and delegates most of the functionality to the Double
// implementation.
public class Temperature : IComparable, IFormattable
{
    // IComparable.CompareTo implementation.
    public int CompareTo(object obj) {
        if (obj == null) return 1;

        Temperature temp = obj as Temperature;
        if (obj != null)
            return m_value.CompareTo(temp.m_value);
        else
            throw new ArgumentException("object is not a Temperature");	
    }

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

    // Parses the temperature from a string in the form
    // [ws][sign]digits['F|'C][ws]
    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;
        }
    }
}
// The Temperature class stores the temperature as a Double
// and delegates most of the functionality to the Double
// implementation.
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.

    // Parses the temperature from a string in the form
    // [ws][sign]digits['F|'C][ws]
    static member Parse(s: string, styles: NumberStyles, provider: IFormatProvider) =
        let temp = Temperature()

        if s.TrimEnd(null).EndsWith "'F" then
            temp.Value <- Double.Parse(s.Remove(s.LastIndexOf '\'', 2), styles, provider)
        elif s.TrimEnd(null).EndsWith "'C" then
            temp.Celsius <- Double.Parse(s.Remove(s.LastIndexOf '\'', 2), styles, provider)
        else
            temp.Value <- Double.Parse(s, styles, provider)
        temp

    interface IComparable with
        // IComparable.CompareTo implementation.
        member this.CompareTo(obj: obj) =
            match obj with 
            | null -> 1
            | :? Temperature as temp ->
                this.Value.CompareTo temp.Value
            | _ ->
                invalidArg "obj" "object is not a Temperature"

    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)
' 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().EndsWith("'F") Then
            temp.Value = Double.Parse(s.Remove(s.LastIndexOf("'"c), 2), styles, provider)
        Else
            If s.TrimEnd().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

注解

Double 值类型表示一个双精度 64 位数字,其值范围介于负 1.79769313486232e308 到正 1.79769313486232e308,且可为正数或负数零、PositiveInfinityNegativeInfinity,但不是数字 (NaN)。 它旨在表示非常大的值(如行星或星系之间的距离)或极小(如物质的分子质量(以公斤为单位)和通常不精确(如地球与另一个太阳系的距离)。 Double 类型符合针对二进制浮点运算的 IEC 60559:1989 (IEEE 754) 标准。

浮点表示形式和精度

Double 数据类型能以 64 位二进制格式存储双精度浮点值,如下表所示:

部件 比特
有效数或尾数 0-51
Exponent 52-62
符号(0 = 正,1 = 负) 63

正如十进制小数无法精确表示某些小数值(如 1/3 或 Math.PI),二进制小数也无法表示某些小数值。 例如,1/10 可用 .1 精确表示为一个十进制小数,用 .001100110011 表示为一个二进制小数,并采用会重复到无穷大的“0011”模式。 在此情况下,浮点值提供了它所表示的数字的非精确表示形式。 对原始浮点值执行其他数学运算通常可能会加剧导致其精度不足。 例如,如果将 .1 乘以 10 和将 .1 连续加 9 次的结果进行比较,可以看到加法因为多涉及 8 次运算而产生了不太精确的结果。 (在 .NET 10 之前,只有当使用“R”Double显示两个值时,这种差异才会明显,因为这会显示出Double类型最多支持的 17 位精度。

using System;

public class Example13
{
    public static void Main()
    {
        Double value = .1;
        Double result1 = value * 10;
        Double result2 = 0;
        for (int ctr = 1; ctr <= 10; ctr++)
            result2 += value;

        Console.WriteLine($".1 * 10:           {result1:R}");
        Console.WriteLine($".1 Added 10 times: {result2:R}");
    }
}
// The example displays the following output:
//       .1 * 10:           1
//       .1 Added 10 times: 0.99999999999999989
let value = 0.1
let result1 = value * 10.
let mutable result2 = 0.
for i = 1 to 10 do
    result2 <- result2 + value

printfn $".1 * 10:           {result1:R}"
printfn $".1 Added 10 times: {result2:R}"
// The example displays the following output:
//       .1 * 10:           1
//       .1 Added 10 times: 0.99999999999999989
Module Example14
    Public Sub Run()
        Dim value As Double = 0.1
        Dim result1 As Double = value * 10
        Dim result2 As Double
        For ctr As Integer = 1 To 10
            result2 += value
        Next
        Console.WriteLine(".1 * 10:           {0:R}", result1)
        Console.WriteLine(".1 Added 10 times: {0:R}", result2)
    End Sub
End Module
' The example displays the following output:
'       .1 * 10:           1
'       .1 Added 10 times: 0.99999999999999989

由于某些数字不能完全表示为小数二进制值,因此浮点数只能近似于实数。

所有浮点数的有效位数也有限,而这也决定了浮点值近似实数的精度。 Double 值的精度最高为 15 位十进制,但内部最多可保持 17 位。 这意味着某些浮点操作可能缺少更改浮点值的精度。 下面的示例进行了这方面的演示。 它定义了一个非常大的浮点值,然后将 Double.Epsilon 的乘积与 1 千万亿相加。 但是,乘积太小,因而无法修改原始浮点值。 其最低有效位为千分之一,而乘积中最高有效位为 10-309

using System;

public class Example14
{
    public static void Main()
    {
        Double value = 123456789012.34567;
        Double additional = Double.Epsilon * 1e15;
        Console.WriteLine($"{value} + {additional} = {value + additional}");
    }
}
// The example displays the following output:
//    123456789012.346 + 4.94065645841247E-309 = 123456789012.346
open System

let value = 123456789012.34567
let additional = Double.Epsilon * 1e15
printfn $"{value} + {additional} = {value + additional}"
// The example displays the following output:
//    123456789012.346 + 4.94065645841247E-309 = 123456789012.346
Module Example15
    Public Sub Run()
        Dim value As Double = 123456789012.34567
        Dim additional As Double = Double.Epsilon * 1.0E+15
        Console.WriteLine("{0} + {1} = {2}", value, additional,
                                           value + additional)
    End Sub
End Module
' The example displays the following output:
'   123456789012.346 + 4.94065645841247E-309 = 123456789012.346

浮点数的有限精度会产生以下几种后果:

  • 对于特定精度而言看似相等的两个浮点数可能不相等,因为它们的最低有效位存在差异。 在以下示例中,会将一系列数字相加,并将它们的总数与预期的总数进行比较。

    using System;
    
    public class Example10
    {
        public static void Main()
        {
            Double[] values = { 10.0, 2.88, 2.88, 2.88, 9.0 };
            Double result = 27.64;
            Double total = 0;
            foreach (var value in values)
                total += value;
    
            if (total.Equals(result))
                Console.WriteLine("The sum of the values equals the total.");
            else
                Console.WriteLine($"The sum of the values ({total}) does not equal the total ({result}).");
        }
    }
    // The example displays the following output:
    //      The sum of the values (36.64) does not equal the total (36.64).
    //
    // If the index items in the Console.WriteLine statement are changed to {0:R},
    // the example displays the following output:
    //       The sum of the values (27.639999999999997) does not equal the total (27.64).
    
    let values = [ 10.0; 2.88; 2.88; 2.88; 9.0 ]
    let result = 27.64
    let total = List.sum values
    
    if total.Equals result then
        printfn "The sum of the values equals the total."
    else
        printfn $"The sum of the values ({total}) does not equal the total ({result})."
    // The example displays the following output:
    //      The sum of the values (36.64) does not equal the total (36.64).
    //
    // If the index items in the Console.WriteLine statement are changed to {0:R},
    // the example displays the following output:
    //       The sum of the values (27.639999999999997) does not equal the total (27.64).
    
    Module Example11
        Public Sub Run()
            Dim values() As Double = {10.0, 2.88, 2.88, 2.88, 9.0}
            Dim result As Double = 27.64
            Dim total As Double
            For Each value In values
                total += value
            Next
            If total.Equals(result) Then
                Console.WriteLine("The sum of the values equals the total.")
            Else
                Console.WriteLine("The sum of the values ({0}) does not equal the total ({1}).",
                               total, result)
            End If
        End Sub
    End Module
    ' The example displays the following output:
    '      The sum of the values (36.64) does not equal the total (36.64).   
    '
    ' If the index items in the Console.WriteLine statement are changed to {0:R},
    ' the example displays the following output:
    '       The sum of the values (27.639999999999997) does not equal the total (27.64).
    

    由于加法运算期间精度损失,这两个值不相等。 在此情况下,可通过调用 Math.Round(Double, Int32) 方法在执行比较之前将 Double 值舍入为所需的精度来解决此问题。

  • 如果使用十进制数,则使用浮点数的数学运算或比较运算可能不会产生相同的结果,因为二进制浮点数可能不等于十进制数。 前一示例通过显示 .1 乘以 10 并加 .1 倍的结果来说明这一点。

    当具有小数值的数值运算的准确性很重要时,可以使用 Decimal 类型而不是 Double 类型。 当使用超过 Int128UInt128 类型范围的整数值的数值运算的精度很重要时,请使用 BigInteger 类型。

  • Single 值的精度低于 Double 值。 Single转换为看似等效Double的值通常不等于Double值,因为精度差异。 在以下示例中,相同除法运算的结果会赋给 DoubleSingle 值。 将 Single 值强制转换为 Double 后,两个值的比较表明它们不相等。

    using System;
    
    public class Example9
    {
        public static void Run()
        {
            double value1 = 1 / 3.0;
            float sValue2 = 1 / 3.0f;
            double value2 = (double)sValue2;
            Console.WriteLine($"{value1:R} = {value2:R}: {value1.Equals(value2)}");
        }
    }
    
    // The example displays the following output:
    //        0.33333333333333331 = 0.3333333432674408: False
    
    open System
    
    let value1 = 1. / 3.
    let sValue2 = 1f /3f
    
    let value2 = double sValue2
    printfn $"{value1:R} = {value2:R}: {value1.Equals value2}"
    // The example displays the following output:
    //        0.33333333333333331 = 0.3333333432674408: False
    
    Module Example10
        Public Sub Run()
            Dim value1 As Double = 1 / 3
            Dim sValue2 As Single = 1 / 3
            Dim value2 As Double = CDbl(sValue2)
            Console.WriteLine("{0} = {1}: {2}", value1, value2, value1.Equals(value2))
        End Sub
    End Module
    ' The example displays the following output:
    '       0.33333333333333331 = 0.3333333432674408: False
    

    要避免此问题,请使用 Double 数据类型代替 Single 数据类型,或使用 Round 方法以使两个值具有相同的精度。

此外,Double类型精度丢失可能会导致在不同平台上,具有Double值的算术和赋值运算结果略有差异。 例如,分配文本Double值的结果可能与 32 位和 64 位版本的.NET不同。 以下示例说明,当将文本值 -4.42330604244772E-305 和值为 -4.42330604244772E-305 的变量分配给 Double 变量时,会出现这种差异。 请注意,在此情况下,Parse(String) 方法的结果不会受到精度损失的影响。

double value = -4.42330604244772E-305;

double fromLiteral = -4.42330604244772E-305;
double fromVariable = value;
double fromParse = double.Parse("-4.42330604244772E-305");

Console.WriteLine("Double value from literal: {0,29:R}", fromLiteral);
Console.WriteLine("Double value from variable: {0,28:R}", fromVariable);
Console.WriteLine("Double value from Parse method: {0,24:R}", fromParse);

// The output is:
//    Double value from literal:        -4.42330604244772E-305
//    Double value from variable:       -4.42330604244772E-305
//    Double value from Parse method:   -4.42330604244772E-305
let value = -4.42330604244772E-305

let fromLiteral = -4.42330604244772E-305
let fromVariable = value
let fromParse = Double.Parse "-4.42330604244772E-305"

printfn $"Double value from literal: {fromLiteral,29:R}"
printfn $"Double value from variable: {fromVariable,28:R}"
printfn $"Double value from Parse method: {fromParse,24:R}"
// On 32-bit versions of the .NET Framework, the output is:
//    Double value from literal:        -4.42330604244772E-305
//    Double value from variable:       -4.42330604244772E-305
//    Double value from Parse method:   -4.42330604244772E-305
//
// On other versions of the .NET Framework, the output is:
//    Double value from literal:      -4.4233060424477198E-305
//    Double value from variable:     -4.4233060424477198E-305
//    Double value from Parse method:   -4.42330604244772E-305
Dim value As Double = -4.4233060424477198E-305

Dim fromLiteral As Double = -4.4233060424477198E-305
Dim fromVariable As Double = value
Dim fromParse As Double = Double.Parse("-4.42330604244772E-305")

Console.WriteLine("Double value from literal: {0,29:R}", fromLiteral)
Console.WriteLine("Double value from variable: {0,28:R}", fromVariable)
Console.WriteLine("Double value from Parse method: {0,24:R}", fromParse)

' The output is:
'    Double value from literal:        -4.42330604244772E-305
'    Double value from variable:       -4.42330604244772E-305
'    Double value from Parse method:   -4.42330604244772E-305

相等性测试

要被视为相等,两个 Double 值必须表示相同的值。 但是,由于值之间的精度差异,或是因为一个或两个值的精度损失,本应相同的浮点值通常会由于最低有效位数的差异而变得不相等。 因此,调用 Equals 方法来确定两个值是否相等,或调用 CompareTo 方法来确定两个 Double 值之间的关系,通常会产生意外的结果。 此问题在以下示例中十分明显;其中,两个明显相等的 Double 值的结果并不相等,因为第一个值的精度为 15 位,而第二个值的精度为 17 位。

using System;

public class Example
{
   public static void Main()
   {
      double value1 = .333333333333333;
      double value2 = 1.0/3;
      Console.WriteLine($"{value1} = {value2}: {value1.Equals(value2)}");
   }
}
// The example displays the following output:
//        0.333333333333333 = 0.33333333333333331: False
open System

let value1 = 0.333333333333333
let value2 = 1. / 3.
printfn $"{value1:R} = {value2:R}: {value1.Equals value2}"
// The example displays the following output:
//        0.333333333333333 = 0.33333333333333331: False
Module Example1
    Public Sub Run()
        Dim value1 As Double = 0.333333333333333
        Dim value2 As Double = 1 / 3
        Console.WriteLine("{0} = {1}: {2}", value1, value2, value1.Equals(value2))
    End Sub
End Module

' The example displays the following output:
'       0.333333333333333 = 0.33333333333333331: False

在精度损失可能会影响比较结果的情况下,可采用以下任意替代方法来调用 EqualsCompareTo 方法:

  • 调用 Math.Round 方法以确保两个值具有相同的精度。 以下示例修改了前一示例以使用此方法,以使两个小数值等效。

    double value1 = .333333333333333;
    double value2 = 1.0 / 3;
    int precision = 7;
    value1 = Math.Round(value1, precision);
    value2 = Math.Round(value2, precision);
    Console.WriteLine($"{value1} = {value2}: {value1.Equals(value2)}");
    
    // The example displays the following output:
    //        0.3333333 = 0.3333333: True
    
    open System
    
    let v1 = 0.333333333333333
    let v2 = 1. / 3.
    let precision = 7
    let value1 = Math.Round(v1, precision)
    let value2 = Math.Round(v2, precision)
    printfn $"{value1:R} = {value2:R}: {value1.Equals value2}"
    // The example displays the following output:
    //        0.3333333 = 0.3333333: True
    
    Module Example3
        Public Sub Run()
            Dim value1 As Double = 0.333333333333333
            Dim value2 As Double = 1 / 3
            Dim precision As Integer = 7
            value1 = Math.Round(value1, precision)
            value2 = Math.Round(value2, precision)
            Console.WriteLine("{0} = {1}: {2}", value1, value2, value1.Equals(value2))
        End Sub
    End Module
    
    ' The example displays the following output:
    '       0.3333333 = 0.3333333: True
    

    精度问题仍适用于中点值的舍入。 有关更多信息,请参见 Math.Round(Double, Int32, MidpointRounding) 方法。

  • 测试近似相等性,而不是相等性。 此方法要求定义一个两个值可以相差但仍然相等的绝对量,或是定义一个较小值与较大值可以相差的相对量。

    Warning

    Double.Epsilon 有时会在测试相等性时用作两个 Double 值之间差异的绝对度量结果。 但是,Double.Epsilon 测量的是可以在值为零的 Double 中加上或减去的最小可能值。 对于大多数正与负 Double 值,Double.Epsilon 的值太小,因而无法检测到。 因此,除零值外,我们不建议将其用于相等性测试。

    以下示例使用后一种方法来定义 IsApproximatelyEqual 方法,而该方法可用于测试两个值之间的相对差值。 该方法通过除以 Math.Max(value1, value2),使得比较是相对于两者中较大值(按数量级)进行的,从而将结果置于正确的数量级顺序。 如果 Math.Max 返回零(即当一个值为零,另一个值为负时),该方法将回退到 Math.Min(value1, value2),使用非零值作为除数。 它还对比了对 IsApproximatelyEqual 方法和 Equals(Double) 方法的调用结果。

    using System;
    
    public class Example3
    {
        public static void Main()
        {
            double one1 = .1 * 10;
            double one2 = 0;
            for (int ctr = 1; ctr <= 10; ctr++)
                one2 += .1;
    
            Console.WriteLine($"{one1} = {one2}: {one1.Equals(one2)}");
            Console.WriteLine($"{one1} is approximately equal to {one2}: {IsApproximatelyEqual(one1, one2, .000000001)}");
        }
    
        static bool IsApproximatelyEqual(double value1, double value2, double epsilon)
        {
            // If they are equal anyway, just return True.
            if (value1.Equals(value2))
                return true;
    
            // Handle NaN, Infinity.
            if (Double.IsInfinity(value1) | Double.IsNaN(value1))
                return value1.Equals(value2);
            else if (Double.IsInfinity(value2) | Double.IsNaN(value2))
                return value1.Equals(value2);
    
            // Handle zero to avoid division by zero
            double divisor = Math.Max(value1, value2);
            if (divisor.Equals(0))
                divisor = Math.Min(value1, value2);
    
            return Math.Abs((value1 - value2) / divisor) <= epsilon;
        }
    }
    
    // The example displays the following output:
    //       1 = 0.99999999999999989: False
    //       1 is approximately equal to 0.99999999999999989: True
    
    open System
    
    let isApproximatelyEqual (value1: double) (value2: double) (epsilon: double) =
        // If they are equal anyway, just return True.
        if value1.Equals value2 then 
            true
        else
            // Handle NaN, Infinity.
            if Double.IsInfinity value1 || Double.IsNaN value1 then 
                value1.Equals value2
            elif Double.IsInfinity value2 || Double.IsNaN value2 then
                value1.Equals value2
            else
                // Handle zero to avoid division by zero
                let divisor = max value1 value2
                let divisor = 
                    if divisor.Equals 0 then
                        min value1 value2
                    else 
                        divisor
                abs ((value1 - value2) / divisor) <= epsilon
    
    let one1 = 0.1 * 10.
    let mutable one2 = 0.
    for _ = 1 to 10 do
        one2 <- one2 + 0.1
    
    printfn $"{one1:R} = {one2:R}: {one1.Equals one2}"
    printfn $"{one1:R} is approximately equal to {one2:R}: {isApproximatelyEqual one1 one2 0.000000001}"
    
    // The example displays the following output:
    //       1 = 0.99999999999999989: False
    //       1 is approximately equal to 0.99999999999999989: True
    
    Module Example4
        Public Sub Run()
            Dim one1 As Double = 0.1 * 10
            Dim one2 As Double = 0
            For ctr As Integer = 1 To 10
                one2 += 0.1
            Next
            Console.WriteLine("{0} = {1}: {2}", one1, one2, one1.Equals(one2))
            Console.WriteLine("{0} is approximately equal to {1}: {2}",
                            one1, one2,
                            IsApproximatelyEqual(one1, one2, 0.000000001))
        End Sub
    
        Function IsApproximatelyEqual(value1 As Double, value2 As Double,
                                     epsilon As Double) As Boolean
            ' If they are equal anyway, just return True.
            If value1.Equals(value2) Then Return True
    
            ' Handle NaN, Infinity.
            If Double.IsInfinity(value1) Or Double.IsNaN(value1) Then
                Return value1.Equals(value2)
            ElseIf Double.IsInfinity(value2) Or Double.IsNaN(value2) Then
                Return value1.Equals(value2)
            End If
    
            ' Handle zero to avoid division by zero
            Dim divisor As Double = Math.Max(value1, value2)
            If divisor.Equals(0) Then
                divisor = Math.Min(value1, value2)
            End If
    
            Return Math.Abs((value1 - value2) / divisor) <= epsilon
        End Function
    End Module
    
    ' The example displays the following output:
    '       1 = 0.99999999999999989: False
    '       1 is approximately equal to 0.99999999999999989: True
    

浮点值和异常

与整型类型的操作不同,整型类型的操作会在DivideByZeroException时或在OverflowException中出现溢出时引发异常,而具有浮点值的操作则不会引发异常。 相反,在特殊情况下,浮点运算的结果为零、正无穷大、负无穷大或不是数字 (NaN):

  • 如果浮点运算的结果对于目标格式来说太小,则结果为零。 当两个非常小的数字相乘时,可能会出现此情况,如以下示例所示。

    using System;
    
    public class Example6
    {
        public static void Main()
        {
            Double value1 = 1.1632875981534209e-225;
            Double value2 = 9.1642346778e-175;
            Double result = value1 * value2;
            Console.WriteLine($"{value1} * {value2} = {result}");
            Console.WriteLine($"{result} = 0: {result.Equals(0.0)}");
        }
    }
    // The example displays the following output:
    //       1.16328759815342E-225 * 9.1642346778E-175 = 0
    //       0 = 0: True
    
    let value1 = 1.1632875981534209e-225
    let value2 = 9.1642346778e-175
    let result = value1 * value2
    printfn $"{value1} * {value2} = {result}"
    printfn $"{result} = 0: {result.Equals 0.0}"
    // The example displays the following output:
    //       1.16328759815342E-225 * 9.1642346778E-175 = 0
    //       0 = 0: True
    
    Module Example7
        Public Sub Run()
            Dim value1 As Double = 1.1632875981534209E-225
            Dim value2 As Double = 9.1642346778E-175
            Dim result As Double = value1 * value2
            Console.WriteLine("{0} * {1} = {2}", value1, value2, result)
            Console.WriteLine("{0} = 0: {1}", result, result.Equals(0.0))
        End Sub
    End Module
    ' The example displays the following output:
    '       1.16328759815342E-225 * 9.1642346778E-175 = 0
    '       0 = 0: True
    
  • 如果浮点运算的结果大小超出目标格式的范围,则运算结果为 PositiveInfinityNegativeInfinity,具体取决于结果的符号。 导致 Double.MaxValue 溢出的运算的结果为 PositiveInfinity,而导致 Double.MinValue 溢出的运算的结果为 NegativeInfinity,如以下示例所示。

    using System;
    
    public class Example7
    {
        public static void Main()
        {
            Double value1 = 4.565e153;
            Double value2 = 6.9375e172;
            Double result = value1 * value2;
            Console.WriteLine($"PositiveInfinity: {Double.IsPositiveInfinity(result)}");
            Console.WriteLine($"NegativeInfinity: {Double.IsNegativeInfinity(result)}{Environment.NewLine}");
    
            value1 = -value1;
            result = value1 * value2;
            Console.WriteLine($"PositiveInfinity: {Double.IsPositiveInfinity(result)}");
            Console.WriteLine($"NegativeInfinity: {Double.IsNegativeInfinity(result)}");
        }
    }
    
    // The example displays the following output:
    //       PositiveInfinity: True
    //       NegativeInfinity: False
    //
    //       PositiveInfinity: False
    //       NegativeInfinity: True
    
    open System
    
    let value1 = 4.565e153
    let value2 = 6.9375e172
    let result = value1 * value2
    printfn $"PositiveInfinity: {Double.IsPositiveInfinity result}"
    printfn $"NegativeInfinity: {Double.IsNegativeInfinity result}\n"
    
    let value3 = - value1
    let result2 = value2 * value3
    printfn $"PositiveInfinity: {Double.IsPositiveInfinity result2}"
    printfn $"NegativeInfinity: {Double.IsNegativeInfinity result2}"
    
    // The example displays the following output:
    //       PositiveInfinity: True
    //       NegativeInfinity: False
    //
    //       PositiveInfinity: False
    //       NegativeInfinity: True
    
    Module Example8
        Public Sub Run()
            Dim value1 As Double = 4.565E+153
            Dim value2 As Double = 6.9375E+172
            Dim result As Double = value1 * value2
            Console.WriteLine("PositiveInfinity: {0}",
                             Double.IsPositiveInfinity(result))
            Console.WriteLine("NegativeInfinity: {0}",
                            Double.IsNegativeInfinity(result))
            Console.WriteLine()
            value1 = -value1
            result = value1 * value2
            Console.WriteLine("PositiveInfinity: {0}",
                             Double.IsPositiveInfinity(result))
            Console.WriteLine("NegativeInfinity: {0}",
                            Double.IsNegativeInfinity(result))
        End Sub
    End Module
    ' The example displays the following output:
    '       PositiveInfinity: True
    '       NegativeInfinity: False
    '       
    '       PositiveInfinity: False
    '       NegativeInfinity: True
    

    PositiveInfinity 也源于将正被除数除以零,而 NegativeInfinity 源于将负被除数除以零。

  • 如果浮点运算无效,则运算结果为 NaN。 例如,NaN 源自以下运算:

    • 除以零,且被除数为零。 请注意,除以零的其他情况会导致 PositiveInfinityNegativeInfinity

    • 具有无效输入的任意浮点运算。 例如,使用负值调用 Math.Sqrt 方法将返回 NaN,而使用大于 1 或小于负 1 的值调用 Math.Acos 方法也会如此。

    • 参数值为 Double.NaN 的任意运算。

类型转换

Double 结果未定义任何显式或隐式转换运算符;相反,转换会由编译器实现。

将任何基元数值类型的值转换为 Double 是一种扩大转换,因此除非编译器显式需要,否则不需要显式强制转换运算符或调用转换方法。 例如,C# 编译器需要使用类型转换运算符将 Decimal 转换为 Double,而 Visual Basic 编译器则不需要类型转换运算符。 以下示例会将其他基元数值类型的最小值或最大值转换为 Double

dynamic[] values = { Byte.MinValue, Byte.MaxValue, Decimal.MinValue,
                   Decimal.MaxValue, Int16.MinValue, Int16.MaxValue,
                   Int32.MinValue, Int32.MaxValue, Int64.MinValue,
                   Int64.MaxValue, SByte.MinValue, SByte.MaxValue,
                   Single.MinValue, Single.MaxValue, UInt16.MinValue,
                   UInt16.MaxValue, UInt32.MinValue, UInt32.MaxValue,
                   UInt64.MinValue, UInt64.MaxValue };
double dblValue;
foreach (dynamic value in values)
{
    if (value.GetType() == typeof(decimal))
        dblValue = (double)value;
    else
        dblValue = value;
    Console.WriteLine($"{value} ({value.GetType().Name}) --> " +
        $"{dblValue:R} ({dblValue.GetType().Name})");
}

// The example displays the following output:
//    0 (Byte) --> 0 (Double)
//    255 (Byte) --> 255 (Double)
//    -79228162514264337593543950335 (Decimal) --> -7.9228162514264338E+28 (Double)
//    79228162514264337593543950335 (Decimal) --> 7.9228162514264338E+28 (Double)
//    -32768 (Int16) --> -32768 (Double)
//    32767 (Int16) --> 32767 (Double)
//    -2147483648 (Int32) --> -2147483648 (Double)
//    2147483647 (Int32) --> 2147483647 (Double)
//    -9223372036854775808 (Int64) --> -9.2233720368547758E+18 (Double)
//    9223372036854775807 (Int64) --> 9.2233720368547758E+18 (Double)
//    -128 (SByte) --> -128 (Double)
//    127 (SByte) --> 127 (Double)
//    -3.402823E+38 (Single) --> -3.4028234663852886E+38 (Double)
//    3.402823E+38 (Single) --> 3.4028234663852886E+38 (Double)
//    0 (UInt16) --> 0 (Double)
//    65535 (UInt16) --> 65535 (Double)
//    0 (UInt32) --> 0 (Double)
//    4294967295 (UInt32) --> 4294967295 (Double)
//    0 (UInt64) --> 0 (Double)
//    18446744073709551615 (UInt64) --> 1.8446744073709552E+19 (Double)
open System

let values: obj[] = 
    [| Byte.MinValue; Byte.MaxValue; Decimal.MinValue
       Decimal.MaxValue; Int16.MinValue; Int16.MaxValue
       Int32.MinValue; Int32.MaxValue; Int64.MinValue
       Int64.MaxValue; SByte.MinValue; SByte.MaxValue
       Single.MinValue; Single.MaxValue; UInt16.MinValue
       UInt16.MaxValue; UInt32.MinValue, UInt32.MaxValue
       UInt64.MinValue; UInt64.MaxValue |]

for value in values do
    let dblValue = value :?> double
    printfn $"{value} ({value.GetType().Name}) --> {dblValue:R} ({dblValue.GetType().Name})"
// The example displays the following output:
//    0 (Byte) --> 0 (Double)
//    255 (Byte) --> 255 (Double)
//    -79228162514264337593543950335 (Decimal) --> -7.9228162514264338E+28 (Double)
//    79228162514264337593543950335 (Decimal) --> 7.9228162514264338E+28 (Double)
//    -32768 (Int16) --> -32768 (Double)
//    32767 (Int16) --> 32767 (Double)
//    -2147483648 (Int32) --> -2147483648 (Double)
//    2147483647 (Int32) --> 2147483647 (Double)
//    -9223372036854775808 (Int64) --> -9.2233720368547758E+18 (Double)
//    9223372036854775807 (Int64) --> 9.2233720368547758E+18 (Double)
//    -128 (SByte) --> -128 (Double)
//    127 (SByte) --> 127 (Double)
//    -3.402823E+38 (Single) --> -3.4028234663852886E+38 (Double)
//    3.402823E+38 (Single) --> 3.4028234663852886E+38 (Double)
//    0 (UInt16) --> 0 (Double)
//    65535 (UInt16) --> 65535 (Double)
//    0 (UInt32) --> 0 (Double)
//    4294967295 (UInt32) --> 4294967295 (Double)
//    0 (UInt64) --> 0 (Double)
//    18446744073709551615 (UInt64) --> 1.8446744073709552E+19 (Double)
Module Example5
    Public Sub Run()
        Dim values() As Object = {Byte.MinValue, Byte.MaxValue, Decimal.MinValue,
                                 Decimal.MaxValue, Int16.MinValue, Int16.MaxValue,
                                 Int32.MinValue, Int32.MaxValue, Int64.MinValue,
                                 Int64.MaxValue, SByte.MinValue, SByte.MaxValue,
                                 Single.MinValue, Single.MaxValue, UInt16.MinValue,
                                 UInt16.MaxValue, UInt32.MinValue, UInt32.MaxValue,
                                 UInt64.MinValue, UInt64.MaxValue}
        Dim dblValue As Double
        For Each value In values
            dblValue = value
            Console.WriteLine("{0} ({1}) --> {2:R} ({3})",
                           value, value.GetType().Name,
                           dblValue, dblValue.GetType().Name)
        Next
    End Sub
End Module
' The example displays the following output:
'    0 (Byte) --> 0 (Double)
'    255 (Byte) --> 255 (Double)
'    -79228162514264337593543950335 (Decimal) --> -7.9228162514264338E+28 (Double)
'    79228162514264337593543950335 (Decimal) --> 7.9228162514264338E+28 (Double)
'    -32768 (Int16) --> -32768 (Double)
'    32767 (Int16) --> 32767 (Double)
'    -2147483648 (Int32) --> -2147483648 (Double)
'    2147483647 (Int32) --> 2147483647 (Double)
'    -9223372036854775808 (Int64) --> -9.2233720368547758E+18 (Double)
'    9223372036854775807 (Int64) --> 9.2233720368547758E+18 (Double)
'    -128 (SByte) --> -128 (Double)
'    127 (SByte) --> 127 (Double)
'    -3.402823E+38 (Single) --> -3.4028234663852886E+38 (Double)
'    3.402823E+38 (Single) --> 3.4028234663852886E+38 (Double)
'    0 (UInt16) --> 0 (Double)
'    65535 (UInt16) --> 65535 (Double)
'    0 (UInt32) --> 0 (Double)
'    4294967295 (UInt32) --> 4294967295 (Double)
'    0 (UInt64) --> 0 (Double)
'    18446744073709551615 (UInt64) --> 1.8446744073709552E+19 (Double)

此外,SingleSingle.NaNSingle.PositiveInfinitySingle.NegativeInfinity 将分别转换为 Double.NaNDouble.PositiveInfinityDouble.NegativeInfinity

请注意,将某些数值类型的值转换为 Double 值可能涉及精度损失。 如示例所示,将 DecimalInt64UInt64 值转换为 Double 值时,可能会丢失精度。

Double 值转换为任何其他基元数值数据类型的值是一个窄化转换,需要在 C# 中使用强制转换运算符、在 Visual Basic 中使用转换方法,或者调用 Convert 方法。 超出目标数据类型范围的值(由目标类型的 MinValueMaxValue 属性进行定义)的行为如下表所示。

目标类型 Result
任何整型 如果在 checked 上下文中进行转换,则会引发 OverflowException 异常。

如果转换发生在未选中的上下文(C# 中的默认值)中,则此转换运算会成功,但值会溢出。
Decimal 异常 OverflowException
Single Single.NegativeInfinity(负值)。

Single.PositiveInfinity(针对负值)。

此外,在 checked 上下文中转换为整数时,Double.NaNDouble.PositiveInfinityDouble.NegativeInfinity 会引发 OverflowException,但在 unchecked 上下文中转换为整数时,这些值会溢出。 对于转换为 Decimal,它们始终会引发 OverflowException。 对于向 Single 进行转换,它们会分别转换为 Single.NaNSingle.PositiveInfinitySingle.NegativeInfinity

由于将 Double 值转换为另一种数值类型,可能会导致精度损失。 在转换为任何整型类型时,正如示例输出所示,当Double值被舍入(如在 Visual Basic 中)或者经过截断(如在 C# 中)时,小数部分将丢失。 对于转换到 Decimal 值和 Single 值,Double 的值在目标数据类型中可能无法精确表示。

以下示例会将多个 Double 值转换为其他几种数字类型。 转换发生在 Visual Basic 的检查上下文中(默认设置)、C# 的选中上下文中(因为 checked 关键字),以及 F# 中(因为 Checked 模块)。 该示例的输出显示了在 checked 和 unchecked 上下文中转换的结果。 可以在未选中上下文中执行转换:在 Visual Basic 中,通过编译时使用 /removeintchecks+ 编译器开关;在 C# 中,通过注释掉 checked 语句;在 F# 中,通过注释掉 open Checked 语句。

using System;

public class Example5
{
    public static void Main()
    {
        Double[] values = { Double.MinValue, -67890.1234, -12345.6789,
                          12345.6789, 67890.1234, Double.MaxValue,
                          Double.NaN, Double.PositiveInfinity,
                          Double.NegativeInfinity };
        checked
        {
            foreach (var value in values)
            {
                try
                {
                    Int64 lValue = (long)value;
                    Console.WriteLine($"{value} ({value.GetType().Name}) --> {lValue} (0x{lValue:X16}) ({lValue.GetType().Name})");
                }
                catch (OverflowException)
                {
                    Console.WriteLine($"Unable to convert {value} to Int64.");
                }
                try
                {
                    UInt64 ulValue = (ulong)value;
                    Console.WriteLine($"{value} ({value.GetType().Name}) --> {ulValue} (0x{ulValue:X16}) ({ulValue.GetType().Name})");
                }
                catch (OverflowException)
                {
                    Console.WriteLine($"Unable to convert {value} to UInt64.");
                }
                try
                {
                    Decimal dValue = (decimal)value;
                    Console.WriteLine($"{value} ({value.GetType().Name}) --> {dValue} ({dValue.GetType().Name})");
                }
                catch (OverflowException)
                {
                    Console.WriteLine($"Unable to convert {value} to Decimal.");
                }
                try
                {
                    Single sValue = (float)value;
                    Console.WriteLine($"{value} ({value.GetType().Name}) --> {sValue} ({sValue.GetType().Name})");
                }
                catch (OverflowException)
                {
                    Console.WriteLine($"Unable to convert {value} to Single.");
                }
                Console.WriteLine();
            }
        }
    }
}
// The example displays the following output for conversions performed
// in a checked context:
//       Unable to convert -1.79769313486232E+308 to Int64.
//       Unable to convert -1.79769313486232E+308 to UInt64.
//       Unable to convert -1.79769313486232E+308 to Decimal.
//       -1.79769313486232E+308 (Double) --> -Infinity (Single)
//
//       -67890.1234 (Double) --> -67890 (0xFFFFFFFFFFFEF6CE) (Int64)
//       Unable to convert -67890.1234 to UInt64.
//       -67890.1234 (Double) --> -67890.1234 (Decimal)
//       -67890.1234 (Double) --> -67890.13 (Single)
//
//       -12345.6789 (Double) --> -12345 (0xFFFFFFFFFFFFCFC7) (Int64)
//       Unable to convert -12345.6789 to UInt64.
//       -12345.6789 (Double) --> -12345.6789 (Decimal)
//       -12345.6789 (Double) --> -12345.68 (Single)
//
//       12345.6789 (Double) --> 12345 (0x0000000000003039) (Int64)
//       12345.6789 (Double) --> 12345 (0x0000000000003039) (UInt64)
//       12345.6789 (Double) --> 12345.6789 (Decimal)
//       12345.6789 (Double) --> 12345.68 (Single)
//
//       67890.1234 (Double) --> 67890 (0x0000000000010932) (Int64)
//       67890.1234 (Double) --> 67890 (0x0000000000010932) (UInt64)
//       67890.1234 (Double) --> 67890.1234 (Decimal)
//       67890.1234 (Double) --> 67890.13 (Single)
//
//       Unable to convert 1.79769313486232E+308 to Int64.
//       Unable to convert 1.79769313486232E+308 to UInt64.
//       Unable to convert 1.79769313486232E+308 to Decimal.
//       1.79769313486232E+308 (Double) --> Infinity (Single)
//
//       Unable to convert NaN to Int64.
//       Unable to convert NaN to UInt64.
//       Unable to convert NaN to Decimal.
//       NaN (Double) --> NaN (Single)
//
//       Unable to convert Infinity to Int64.
//       Unable to convert Infinity to UInt64.
//       Unable to convert Infinity to Decimal.
//       Infinity (Double) --> Infinity (Single)
//
//       Unable to convert -Infinity to Int64.
//       Unable to convert -Infinity to UInt64.
//       Unable to convert -Infinity to Decimal.
//       -Infinity (Double) --> -Infinity (Single)
// The example displays the following output for conversions performed
// in an unchecked context:
//       -1.79769313486232E+308 (Double) --> -9223372036854775808 (0x8000000000000000) (Int64)
//       -1.79769313486232E+308 (Double) --> 9223372036854775808 (0x8000000000000000) (UInt64)
//       Unable to convert -1.79769313486232E+308 to Decimal.
//       -1.79769313486232E+308 (Double) --> -Infinity (Single)
//
//       -67890.1234 (Double) --> -67890 (0xFFFFFFFFFFFEF6CE) (Int64)
//       -67890.1234 (Double) --> 18446744073709483726 (0xFFFFFFFFFFFEF6CE) (UInt64)
//       -67890.1234 (Double) --> -67890.1234 (Decimal)
//       -67890.1234 (Double) --> -67890.13 (Single)
//
//       -12345.6789 (Double) --> -12345 (0xFFFFFFFFFFFFCFC7) (Int64)
//       -12345.6789 (Double) --> 18446744073709539271 (0xFFFFFFFFFFFFCFC7) (UInt64)
//       -12345.6789 (Double) --> -12345.6789 (Decimal)
//       -12345.6789 (Double) --> -12345.68 (Single)
//
//       12345.6789 (Double) --> 12345 (0x0000000000003039) (Int64)
//       12345.6789 (Double) --> 12345 (0x0000000000003039) (UInt64)
//       12345.6789 (Double) --> 12345.6789 (Decimal)
//       12345.6789 (Double) --> 12345.68 (Single)
//
//       67890.1234 (Double) --> 67890 (0x0000000000010932) (Int64)
//       67890.1234 (Double) --> 67890 (0x0000000000010932) (UInt64)
//       67890.1234 (Double) --> 67890.1234 (Decimal)
//       67890.1234 (Double) --> 67890.13 (Single)
//
//       1.79769313486232E+308 (Double) --> -9223372036854775808 (0x8000000000000000) (Int64)
//       1.79769313486232E+308 (Double) --> 0 (0x0000000000000000) (UInt64)
//       Unable to convert 1.79769313486232E+308 to Decimal.
//       1.79769313486232E+308 (Double) --> Infinity (Single)
//
//       NaN (Double) --> -9223372036854775808 (0x8000000000000000) (Int64)
//       NaN (Double) --> 0 (0x0000000000000000) (UInt64)
//       Unable to convert NaN to Decimal.
//       NaN (Double) --> NaN (Single)
//
//       Infinity (Double) --> -9223372036854775808 (0x8000000000000000) (Int64)
//       Infinity (Double) --> 0 (0x0000000000000000) (UInt64)
//       Unable to convert Infinity to Decimal.
//       Infinity (Double) --> Infinity (Single)
//
//       -Infinity (Double) --> -9223372036854775808 (0x8000000000000000) (Int64)
//       -Infinity (Double) --> 9223372036854775808 (0x8000000000000000) (UInt64)
//       Unable to convert -Infinity to Decimal.
//       -Infinity (Double) --> -Infinity (Single)
open System
open Checked

let values = 
    [| Double.MinValue; -67890.1234; -12345.6789
       12345.6789; 67890.1234; Double.MaxValue
       Double.NaN; Double.PositiveInfinity;
       Double.NegativeInfinity |]

for value in values do
    try
        let lValue = int64 value
        printfn $"{value} ({value.GetType().Name}) --> {lValue} (0x{lValue:X16}) ({lValue.GetType().Name})"
    with :? OverflowException ->
        printfn $"Unable to convert {value} to Int64."
    try
        let ulValue = uint64 value
        printfn $"{value} ({value.GetType().Name}) --> {ulValue} (0x{ulValue:X16}) ({ulValue.GetType().Name})"
    with :? OverflowException ->
        printfn $"Unable to convert {value} to UInt64."
    try
        let dValue = decimal value
        printfn $"{value} ({value.GetType().Name}) --> {dValue} ({dValue.GetType().Name})"
    with :? OverflowException ->
        printfn $"Unable to convert {value} to Decimal."
    try
        let sValue = float32 value
        printfn $"{value} ({value.GetType().Name}) --> {sValue} ({sValue.GetType().Name})"
    with :? OverflowException ->
        printfn $"Unable to convert {value} to Single."
    printfn ""
// The example displays the following output for conversions performed
// in a checked context:
//       Unable to convert -1.79769313486232E+308 to Int64.
//       Unable to convert -1.79769313486232E+308 to UInt64.
//       Unable to convert -1.79769313486232E+308 to Decimal.
//       -1.79769313486232E+308 (Double) --> -Infinity (Single)
//
//       -67890.1234 (Double) --> -67890 (0xFFFFFFFFFFFEF6CE) (Int64)
//       Unable to convert -67890.1234 to UInt64.
//       -67890.1234 (Double) --> -67890.1234 (Decimal)
//       -67890.1234 (Double) --> -67890.13 (Single)
//
//       -12345.6789 (Double) --> -12345 (0xFFFFFFFFFFFFCFC7) (Int64)
//       Unable to convert -12345.6789 to UInt64.
//       -12345.6789 (Double) --> -12345.6789 (Decimal)
//       -12345.6789 (Double) --> -12345.68 (Single)
//
//       12345.6789 (Double) --> 12345 (0x0000000000003039) (Int64)
//       12345.6789 (Double) --> 12345 (0x0000000000003039) (UInt64)
//       12345.6789 (Double) --> 12345.6789 (Decimal)
//       12345.6789 (Double) --> 12345.68 (Single)
//
//       67890.1234 (Double) --> 67890 (0x0000000000010932) (Int64)
//       67890.1234 (Double) --> 67890 (0x0000000000010932) (UInt64)
//       67890.1234 (Double) --> 67890.1234 (Decimal)
//       67890.1234 (Double) --> 67890.13 (Single)
//
//       Unable to convert 1.79769313486232E+308 to Int64.
//       Unable to convert 1.79769313486232E+308 to UInt64.
//       Unable to convert 1.79769313486232E+308 to Decimal.
//       1.79769313486232E+308 (Double) --> Infinity (Single)
//
//       Unable to convert NaN to Int64.
//       Unable to convert NaN to UInt64.
//       Unable to convert NaN to Decimal.
//       NaN (Double) --> NaN (Single)
//
//       Unable to convert Infinity to Int64.
//       Unable to convert Infinity to UInt64.
//       Unable to convert Infinity to Decimal.
//       Infinity (Double) --> Infinity (Single)
//
//       Unable to convert -Infinity to Int64.
//       Unable to convert -Infinity to UInt64.
//       Unable to convert -Infinity to Decimal.
//       -Infinity (Double) --> -Infinity (Single)
// The example displays the following output for conversions performed
// in an unchecked context:
//       -1.79769313486232E+308 (Double) --> -9223372036854775808 (0x8000000000000000) (Int64)
//       -1.79769313486232E+308 (Double) --> 9223372036854775808 (0x8000000000000000) (UInt64)
//       Unable to convert -1.79769313486232E+308 to Decimal.
//       -1.79769313486232E+308 (Double) --> -Infinity (Single)
//
//       -67890.1234 (Double) --> -67890 (0xFFFFFFFFFFFEF6CE) (Int64)
//       -67890.1234 (Double) --> 18446744073709483726 (0xFFFFFFFFFFFEF6CE) (UInt64)
//       -67890.1234 (Double) --> -67890.1234 (Decimal)
//       -67890.1234 (Double) --> -67890.13 (Single)
//
//       -12345.6789 (Double) --> -12345 (0xFFFFFFFFFFFFCFC7) (Int64)
//       -12345.6789 (Double) --> 18446744073709539271 (0xFFFFFFFFFFFFCFC7) (UInt64)
//       -12345.6789 (Double) --> -12345.6789 (Decimal)
//       -12345.6789 (Double) --> -12345.68 (Single)
//
//       12345.6789 (Double) --> 12345 (0x0000000000003039) (Int64)
//       12345.6789 (Double) --> 12345 (0x0000000000003039) (UInt64)
//       12345.6789 (Double) --> 12345.6789 (Decimal)
//       12345.6789 (Double) --> 12345.68 (Single)
//
//       67890.1234 (Double) --> 67890 (0x0000000000010932) (Int64)
//       67890.1234 (Double) --> 67890 (0x0000000000010932) (UInt64)
//       67890.1234 (Double) --> 67890.1234 (Decimal)
//       67890.1234 (Double) --> 67890.13 (Single)
//
//       1.79769313486232E+308 (Double) --> -9223372036854775808 (0x8000000000000000) (Int64)
//       1.79769313486232E+308 (Double) --> 0 (0x0000000000000000) (UInt64)
//       Unable to convert 1.79769313486232E+308 to Decimal.
//       1.79769313486232E+308 (Double) --> Infinity (Single)
//
//       NaN (Double) --> -9223372036854775808 (0x8000000000000000) (Int64)
//       NaN (Double) --> 0 (0x0000000000000000) (UInt64)
//       Unable to convert NaN to Decimal.
//       NaN (Double) --> NaN (Single)
//
//       Infinity (Double) --> -9223372036854775808 (0x8000000000000000) (Int64)
//       Infinity (Double) --> 0 (0x0000000000000000) (UInt64)
//       Unable to convert Infinity to Decimal.
//       Infinity (Double) --> Infinity (Single)
//
//       -Infinity (Double) --> -9223372036854775808 (0x8000000000000000) (Int64)
//       -Infinity (Double) --> 9223372036854775808 (0x8000000000000000) (UInt64)
//       Unable to convert -Infinity to Decimal.
//       -Infinity (Double) --> -Infinity (Single)
Module Example6
    Public Sub Run()
        Dim values() As Double = {Double.MinValue, -67890.1234, -12345.6789,
                                 12345.6789, 67890.1234, Double.MaxValue,
                                 Double.NaN, Double.PositiveInfinity,
                                 Double.NegativeInfinity}
        For Each value In values
            Try
                Dim lValue As Int64 = CLng(value)
                Console.WriteLine("{0} ({1}) --> {2} (0x{2:X16}) ({3})",
                               value, value.GetType().Name,
                               lValue, lValue.GetType().Name)
            Catch e As OverflowException
                Console.WriteLine("Unable to convert {0} to Int64.", value)
            End Try
            Try
                Dim ulValue As UInt64 = CULng(value)
                Console.WriteLine("{0} ({1}) --> {2} (0x{2:X16}) ({3})",
                               value, value.GetType().Name,
                               ulValue, ulValue.GetType().Name)
            Catch e As OverflowException
                Console.WriteLine("Unable to convert {0} to UInt64.", value)
            End Try
            Try
                Dim dValue As Decimal = CDec(value)
                Console.WriteLine("{0} ({1}) --> {2} ({3})",
                               value, value.GetType().Name,
                               dValue, dValue.GetType().Name)
            Catch e As OverflowException
                Console.WriteLine("Unable to convert {0} to Decimal.", value)
            End Try
            Try
                Dim sValue As Single = CSng(value)
                Console.WriteLine("{0} ({1}) --> {2} ({3})",
                               value, value.GetType().Name,
                               sValue, sValue.GetType().Name)
            Catch e As OverflowException
                Console.WriteLine("Unable to convert {0} to Single.", value)
            End Try
            Console.WriteLine()
        Next
    End Sub
End Module
' The example displays the following output for conversions performed
' in a checked context:
'       Unable to convert -1.79769313486232E+308 to Int64.
'       Unable to convert -1.79769313486232E+308 to UInt64.
'       Unable to convert -1.79769313486232E+308 to Decimal.
'       -1.79769313486232E+308 (Double) --> -Infinity (Single)
'
'       -67890.1234 (Double) --> -67890 (0xFFFFFFFFFFFEF6CE) (Int64)
'       Unable to convert -67890.1234 to UInt64.
'       -67890.1234 (Double) --> -67890.1234 (Decimal)
'       -67890.1234 (Double) --> -67890.13 (Single)
'
'       -12345.6789 (Double) --> -12346 (0xFFFFFFFFFFFFCFC6) (Int64)
'       Unable to convert -12345.6789 to UInt64.
'       -12345.6789 (Double) --> -12345.6789 (Decimal)
'       -12345.6789 (Double) --> -12345.68 (Single)
'
'       12345.6789 (Double) --> 12346 (0x000000000000303A) (Int64)
'       12345.6789 (Double) --> 12346 (0x000000000000303A) (UInt64)
'       12345.6789 (Double) --> 12345.6789 (Decimal)
'       12345.6789 (Double) --> 12345.68 (Single)
'
'       67890.1234 (Double) --> 67890 (0x0000000000010932) (Int64)
'       67890.1234 (Double) --> 67890 (0x0000000000010932) (UInt64)
'       67890.1234 (Double) --> 67890.1234 (Decimal)
'       67890.1234 (Double) --> 67890.13 (Single)
'
'       Unable to convert 1.79769313486232E+308 to Int64.
'       Unable to convert 1.79769313486232E+308 to UInt64.
'       Unable to convert 1.79769313486232E+308 to Decimal.
'       1.79769313486232E+308 (Double) --> Infinity (Single)
'
'       Unable to convert NaN to Int64.
'       Unable to convert NaN to UInt64.
'       Unable to convert NaN to Decimal.
'       NaN (Double) --> NaN (Single)
'
'       Unable to convert Infinity to Int64.
'       Unable to convert Infinity to UInt64.
'       Unable to convert Infinity to Decimal.
'       Infinity (Double) --> Infinity (Single)
'
'       Unable to convert -Infinity to Int64.
'       Unable to convert -Infinity to UInt64.
'       Unable to convert -Infinity to Decimal.
'       -Infinity (Double) --> -Infinity (Single)
' The example displays the following output for conversions performed
' in an unchecked context:
'       -1.79769313486232E+308 (Double) --> -9223372036854775808 (0x8000000000000000) (Int64)
'       -1.79769313486232E+308 (Double) --> 9223372036854775808 (0x8000000000000000) (UInt64)
'       Unable to convert -1.79769313486232E+308 to Decimal.
'       -1.79769313486232E+308 (Double) --> -Infinity (Single)
'
'       -67890.1234 (Double) --> -67890 (0xFFFFFFFFFFFEF6CE) (Int64)
'       -67890.1234 (Double) --> 18446744073709483726 (0xFFFFFFFFFFFEF6CE) (UInt64)
'       -67890.1234 (Double) --> -67890.1234 (Decimal)
'       -67890.1234 (Double) --> -67890.13 (Single)
'
'       -12345.6789 (Double) --> -12346 (0xFFFFFFFFFFFFCFC6) (Int64)
'       -12345.6789 (Double) --> 18446744073709539270 (0xFFFFFFFFFFFFCFC6) (UInt64)
'       -12345.6789 (Double) --> -12345.6789 (Decimal)
'       -12345.6789 (Double) --> -12345.68 (Single)
'
'       12345.6789 (Double) --> 12346 (0x000000000000303A) (Int64)
'       12345.6789 (Double) --> 12346 (0x000000000000303A) (UInt64)
'       12345.6789 (Double) --> 12345.6789 (Decimal)
'       12345.6789 (Double) --> 12345.68 (Single)
'
'       67890.1234 (Double) --> 67890 (0x0000000000010932) (Int64)
'       67890.1234 (Double) --> 67890 (0x0000000000010932) (UInt64)
'       67890.1234 (Double) --> 67890.1234 (Decimal)
'       67890.1234 (Double) --> 67890.13 (Single)
'
'       1.79769313486232E+308 (Double) --> -9223372036854775808 (0x8000000000000000) (Int64)
'       1.79769313486232E+308 (Double) --> 0 (0x0000000000000000) (UInt64)
'       Unable to convert 1.79769313486232E+308 to Decimal.
'       1.79769313486232E+308 (Double) --> Infinity (Single)
'
'       NaN (Double) --> -9223372036854775808 (0x8000000000000000) (Int64)
'       NaN (Double) --> 0 (0x0000000000000000) (UInt64)
'       Unable to convert NaN to Decimal.
'       NaN (Double) --> NaN (Single)
'
'       Infinity (Double) --> -9223372036854775808 (0x8000000000000000) (Int64)
'       Infinity (Double) --> 0 (0x0000000000000000) (UInt64)
'       Unable to convert Infinity to Decimal.
'       Infinity (Double) --> Infinity (Single)
'
'       -Infinity (Double) --> -9223372036854775808 (0x8000000000000000) (Int64)
'       -Infinity (Double) --> 9223372036854775808 (0x8000000000000000) (UInt64)
'       Unable to convert -Infinity to Decimal.
'       -Infinity (Double) --> -Infinity (Single)

有关数值类型转换的详细信息,请参阅 .NET 中的 Type ConversionType Conversion Tables

浮点功能

Double 结构和相关类型提供了执行以下领域的运算的方法:

  • 值的比较。 你可调用 Equals 方法来确定两个 Double 值是否相等,也可调用 CompareTo 方法来确定两个值之间的关系。

    Double 结构还支持一整套比较运算符。 例如,可测试相等或不相等,或者确定一个值是大于还是等于另一个值。 如果某一操作数是 Double 以外的数值类型,则会在执行比较之前将其转换为 Double

    Warning

    由于精度差异,预期相等的两 Double 个值可能不相等,这会影响比较结果。 有关比较两 Double 个值的信息,请参阅 “测试是否相等 ”部分。

    此外,还可调用 IsNaNIsInfinityIsPositiveInfinityIsNegativeInfinity 方法来针对这些特殊值进行测试。

  • 数学运算。 常见的算术运算(如加、减、乘和除)将由语言编译器和公共中间语言 (CIL) 指令实现,而不是由 Double 方法实现。 如果数学运算中的其中一个操作数是非数值类型 Double,则会在执行该操作之前将其转换为一个 Double 操作数。 此运算的结果也是 Double 值。

    可以通过在 static 类 Visual Basic中调用 SharedSystem.Math) 方法来执行其他数学运算。 其中包括通常用于算术(如 Math.AbsMath.SignMath.Sqrt)、几何(如 Math.CosMath.Sin)以及微积分(如 Math.Log)的其他方法。

    还可操控 Double 值中的各个位。 BitConverter.DoubleToInt64Bits 方法会在 64 位整数中保留 Double 值的位模式。 BitConverter.GetBytes(Double) 方法会在字节数组中返回其位模式。

  • 舍入。 舍入技术通常会用作减少因浮点表示和精度问题所导致的值差异影响。 可以调用 Double 方法来舍入 Math.Round 值。

  • 格式设置。 可通过调用 Double 方法或使用复合格式设置功能,将 ToString 值转换为其字符串表示形式。 有关格式字符串如何控制浮点值的字符串表示形式的信息,请参阅标准数值格式字符串自定义数字格式字符串

  • 分析字符串。 可通过调用 DoubleParse 方法将浮点值的字符串表示形式转换为 TryParse 值。 如果分析操作失败,Parse 方法则会引发异常,而 TryParse 方法会返回 false

  • 类型转换Double 结构为 IConvertible 接口提供显式接口实现,该接口支持在任意两种标准.NET数据类型之间进行转换。 语言编译器还支持将所有其他标准数值类型的值隐式转换为 Double 值。 将任何标准数值类型的值转换为 Double 是一种扩大转换,不需要使用强制转换运算符或转换方法。

    但是,Int64Single 值的转换可能涉及精度损失。 下表列出了针对其中每种类型的精度差异:

    类型 最大精度 内部精度
    Double 15 17
    Int64 19 位十进制数字 19 位十进制数字
    Single 7 位十进制数字 9 位十进制数字

    精度问题最常影响被转换为 Single 值的 Double 值。 在以下示例中,相同的除法运算生成的两个值不相等,因为其中一个值是转换为 Double 的单精度浮点值。

    using System;
    
    public class Example13
    {
        public static void Main()
        {
            Double value = .1;
            Double result1 = value * 10;
            Double result2 = 0;
            for (int ctr = 1; ctr <= 10; ctr++)
                result2 += value;
    
            Console.WriteLine($".1 * 10:           {result1:R}");
            Console.WriteLine($".1 Added 10 times: {result2:R}");
        }
    }
    // The example displays the following output:
    //       .1 * 10:           1
    //       .1 Added 10 times: 0.99999999999999989
    
    let value = 0.1
    let result1 = value * 10.
    let mutable result2 = 0.
    for i = 1 to 10 do
        result2 <- result2 + value
    
    printfn $".1 * 10:           {result1:R}"
    printfn $".1 Added 10 times: {result2:R}"
    // The example displays the following output:
    //       .1 * 10:           1
    //       .1 Added 10 times: 0.99999999999999989
    
    Module Example14
        Public Sub Run()
            Dim value As Double = 0.1
            Dim result1 As Double = value * 10
            Dim result2 As Double
            For ctr As Integer = 1 To 10
                result2 += value
            Next
            Console.WriteLine(".1 * 10:           {0:R}", result1)
            Console.WriteLine(".1 Added 10 times: {0:R}", result2)
        End Sub
    End Module
    ' The example displays the following output:
    '       .1 * 10:           1
    '       .1 Added 10 times: 0.99999999999999989
    

字段

名称 说明
E

表示由常量指定的自然对数基数,e.

Epsilon

表示大于零的最小正 Double 值。 此字段为常量。

MaxValue

表示 a Double. 的最大可能值。 此字段为常量。

MinValue

表示一个 Double最小可能的值。 此字段为常量。

NaN

表示不是数字的值(NaN)。 此字段为常量。

NegativeInfinity

表示负无穷大。 此字段为常量。

NegativeZero

表示负零(-0)。

Pi

表示圆的周长与其直径的比率,由常量指定,π。

PositiveInfinity

表示正无穷大。 此字段为常量。

Tau

表示一个轮次的弧度数,由常量 τ 指定。

方法

名称 说明
Abs(Double)

计算值的绝对值。

Acos(Double)

计算值的反余弦值。

Acosh(Double)

计算值的双曲反余弦值。

AcosPi(Double)

计算值的反余弦值,并将结果除以 pi

Asin(Double)

计算值的反正弦值。

Asinh(Double)

计算值的双曲反正弦值。

AsinPi(Double)

计算值的反正弦值,并将结果除以 pi

Atan(Double)

计算值的反正切值。

Atan2(Double, Double)

计算两个值的商的反正切值。

Atan2Pi(Double, Double)

计算两个值的商的反正切值,并将结果除以 pi

Atanh(Double)

计算值的双曲反正切值。

AtanPi(Double)

计算值的反正切值,并将结果除以 pi。

BitDecrement(Double)

返回与指定值进行比较的最大值。

BitIncrement(Double)

返回与指定值进行比较的最小值。

Cbrt(Double)

计算值的多维数据集根。

Ceiling(Double)

计算值的上限。

Clamp(Double, Double, Double)

将值固定到非独占最小值和最大值。

ClampNative(Double, Double, Double)

使用特定于平台的行为NaNNegativeZero,将值固定到非独占的最小值和最大值。

CompareTo(Double)

将此实例与指定的双精度浮点数进行比较,并返回一个整数,该整数指示此实例的值是小于、等于还是大于指定的双精度浮点数的值。

CompareTo(Object)

将此实例与指定的对象进行比较,并返回一个整数,该整数指示此实例的值是小于、等于还是大于指定对象的值。

ConvertToInteger<TInteger>(Double)

使用溢出时饱和度将值转换为指定的整数类型

ConvertToIntegerNative<TInteger>(Double)

使用特定于平台的行为在溢出时将值转换为指定的整数类型。

CopySign(Double, Double)

将值的符号复制到另一个值的符号。

Cos(Double)

计算值的余弦值。

Cosh(Double)

计算值的双曲余弦值。

CosPi(Double)

计算已由 pi其乘数的值的余弦值。

CreateChecked<TOther>(TOther)

从值创建当前类型的实例,为超出当前类型的可表示范围的任何值引发溢出异常。

CreateSaturating<TOther>(TOther)

从值创建当前类型的实例,使属于当前类型的可表示范围之外的任何值饱和。

CreateTruncating<TOther>(TOther)

从值创建当前类型的实例,截断属于当前类型的可表示范围之外的任何值。

DegreesToRadians(Double)

将给定值从度转换为弧度。

Equals(Double)

返回一个值,该值指示此实例和指定的 Double 对象是否表示相同的值。

Equals(Object)

返回一个值,该值指示此实例是否等于指定的对象。

Exp(Double)

E计算提升到给定功率。

Exp10(Double)

10计算提升到给定功率。

Exp10M1(Double)

10计算提升到给定的幂并减去一个。

Exp2(Double)

2计算提升到给定功率。

Exp2M1(Double)

2计算提升到给定的幂并减去一个。

ExpM1(Double)

E计算提升到给定的幂并减去一个。

Floor(Double)

计算值的下限。

FusedMultiplyAdd(Double, Double, Double)

计算三个值的融合乘法。

GetHashCode()

返回此实例的哈希代码。

GetTypeCode()

返回 TypeCode 值类型的 Double值 。

Hypot(Double, Double)

计算给定的两个值,该值表示右角三角形中较短边的长度。

Ieee754Remainder(Double, Double)

计算 IEEE 754 指定的两个值的余数。

ILogB(Double)

计算值的整数对数。

IsEvenInteger(Double)

确定值是否表示偶数。

IsFinite(Double)

确定指定的值是有限值(零、非正常值还是正常值)。

IsInfinity(Double)

返回一个值,该值指示指定的数字的计算结果为负数还是正无穷大。

IsInteger(Double)

确定值是否表示整型值。

IsNaN(Double)

返回一个值,该值指示指定的值是否不是数字(NaN)。

IsNegative(Double)

确定指定的值是否为负值。

IsNegativeInfinity(Double)

返回一个值,该值指示指定的数字是否计算为负无穷大。

IsNormal(Double)

确定指定的值是否正常。

IsOddInteger(Double)

确定值是否表示奇数整数。

IsPositive(Double)

确定值是否为正值。

IsPositiveInfinity(Double)

返回一个值,该值指示指定的数字是否计算为正无穷大。

IsPow2(Double)

确定值是否为 2 的幂。

IsRealNumber(Double)

确定值是否表示实数。

IsSubnormal(Double)

确定指定的值是否为非正常值。

Lerp(Double, Double, Double)

根据给定权重在两个值之间执行线性内插。

Log(Double, Double)

计算指定基中的值的对数。

Log(Double)

计算值的自然(base-E 对数)。

Log10(Double)

计算值的 base-10 对数。

Log10P1(Double)

计算值加 10 的 base-10 对数。

Log2(Double)

计算值的 log2。

Log2P1(Double)

计算值加 1 的 base-2 对数。

LogP1(Double)

计算值加 1 的自然(base-E) 对数。

Max(Double, Double)

比较两个值与计算值更大。

MaxMagnitude(Double, Double)

比较两个值与计算值更大。

MaxMagnitudeNumber(Double, Double)

将两个值与计算进行比较,如果输入 NaN是,则返回另一个值。

MaxNative(Double, Double)

比较两个值与使用特定于平台的行为 NaNNegativeZero更大的计算值。

MaxNumber(Double, Double)

将两个值与计算进行比较,如果输入 NaN是,则返回另一个值。

Min(Double, Double)

比较两个值与计算值较小。

MinMagnitude(Double, Double)

比较两个值与计算值较小。

MinMagnitudeNumber(Double, Double)

将两个值与计算进行比较,如果输入 NaN是,则返回另一个值。

MinNative(Double, Double)

将两个值与计算进行比较,后者使用特定于平台的行为 NaNNegativeZero

MinNumber(Double, Double)

将两个值与计算进行比较,如果输入 NaN较小,则返回另一个值。

MultiplyAddEstimate(Double, Double, Double)

计算估计值 (left * right) + 。 addend

Parse(ReadOnlySpan<Byte>, IFormatProvider)

将 UTF-8 字符的范围分析为值。

Parse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider)

将 UTF-8 字符的范围分析为值。

Parse(ReadOnlySpan<Char>, IFormatProvider)

将字符的范围分析为值。

Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider)

将包含指定样式和区域性特定格式的数字的字符串表示形式的字符范围转换为等效的双精度浮点数。

Parse(String, IFormatProvider)

将指定区域性特定格式的数字的字符串表示形式转换为等效的双精度浮点数。

Parse(String, NumberStyles, IFormatProvider)

将指定样式和区域性特定格式的数字的字符串表示形式转换为等效的双精度浮点数。

Parse(String, NumberStyles)

将指定样式中的数字的字符串表示形式转换为等效的双精度浮点数。

Parse(String)

将数字的字符串表示形式转换为等效的双精度浮点数。

Pow(Double, Double)

计算提升到给定功率的值。

RadiansToDegrees(Double)

将给定值从弧度转换为度。

ReciprocalEstimate(Double)

计算值的对等值的估计值。

ReciprocalSqrtEstimate(Double)

计算值的倒数平方根的估计值。

RootN(Double, Int32)

计算值的第 n 个根。

Round(Double, Int32, MidpointRounding)

使用默认舍入模式将值舍入为指定数量的小数位数(ToEven)。

Round(Double, Int32)

使用默认舍入模式将值舍入为指定数量的小数位数(ToEven)。

Round(Double, MidpointRounding)

使用指定的舍入模式将值舍入到最接近的整数。

Round(Double)

使用默认舍入模式将值舍入到最接近的整数(ToEven)。

ScaleB(Double, Int32)

计算一个值及其基弧度升至指定功率的乘积。

Sign(Double)

计算值的符号。

Sin(Double)

计算值的正弦值。

SinCos(Double)

计算值的正弦值和余弦值。

SinCosPi(Double)

计算值的正弦值和余弦值。

Sinh(Double)

计算值的双曲正弦值。

SinPi(Double)

计算乘以 pi的值的正弦值。

Sqrt(Double)

计算值的平方根。

Tan(Double)

计算值的正切值。

Tanh(Double)

计算值的双曲正切值。

TanPi(Double)

计算已由 pi其乘数的值的正切值。

ToString()

将此实例的数值转换为其等效的字符串表示形式。

ToString(IFormatProvider)

使用指定的区域性特定格式信息将此实例的数值转换为其等效的字符串表示形式。

ToString(String, IFormatProvider)

使用指定的格式和区域性特定的格式信息将此实例的数值转换为其等效的字符串表示形式。

ToString(String)

使用指定的格式将此实例的数值转换为其等效的字符串表示形式。

Truncate(Double)

截断值。

TryFormat(Span<Byte>, Int32, ReadOnlySpan<Char>, IFormatProvider)

尝试将当前实例的值格式化为 UTF-8,并将其设置为提供的字节范围。

TryFormat(Span<Char>, Int32, ReadOnlySpan<Char>, IFormatProvider)

尝试将当前双精度实例的值格式化为提供的字符范围。

TryParse(ReadOnlySpan<Byte>, Double)

尝试将包含数字字符串表示形式的 UTF-8 字符范围转换为其等效的双精度浮点数。

TryParse(ReadOnlySpan<Byte>, IFormatProvider, Double)

尝试将 UTF-8 字符的范围分析为值。

TryParse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider, Double)

尝试将 UTF-8 字符的范围分析为值。

TryParse(ReadOnlySpan<Char>, Double)

将指定样式和区域性特定格式的数字的跨度表示形式转换为其等效的双精度浮点数。 返回值指示转换是成功还是失败。

TryParse(ReadOnlySpan<Char>, IFormatProvider, Double)

尝试将字符范围分析为值。

TryParse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider, Double)

将包含指定样式和区域性特定格式的数字的字符串表示形式的字符范围转换为其等效的双精度浮点数。 返回值指示转换是成功还是失败。

TryParse(String, Double)

将数字的字符串表示形式转换为等效的双精度浮点数。 返回值指示转换是成功还是失败。

TryParse(String, IFormatProvider, Double)

尝试将字符串分析为值。

TryParse(String, NumberStyles, IFormatProvider, Double)

将指定样式和区域性特定格式的数字的字符串表示形式转换为等效的双精度浮点数。 返回值指示转换是成功还是失败。

运营商

名称 说明
Equality(Double, Double)

返回一个值,该值指示两个指定 Double 值是否相等。

GreaterThan(Double, Double)

返回一个值,该值指示指定的 Double 值是否大于另一个指定 Double 值。

GreaterThanOrEqual(Double, Double)

返回一个值,该值指示指定的 Double 值是否大于或等于另一个指定 Double 值。

Inequality(Double, Double)

返回一个值,该值指示两个指定的 Double 值是否不相等。

LessThan(Double, Double)

返回一个值,该值指示指定的 Double 值是否小于另一个指定 Double 值。

LessThanOrEqual(Double, Double)

返回一个值,该值指示指定的 Double 值是否小于或等于另一个指定 Double 值。

显式接口实现

名称 说明
IAdditionOperators<Double,Double,Double>.Addition(Double, Double)

将两个值相加以计算其总和。

IAdditiveIdentity<Double,Double>.AdditiveIdentity

获取当前类型的累加标识。

IBinaryNumber<Double>.AllBitsSet

获取在其中设置所有位的二进制类型的实例。

IBitwiseOperators<Double,Double,Double>.BitwiseAnd(Double, Double)

计算两个值的按位和两个值。

IBitwiseOperators<Double,Double,Double>.BitwiseOr(Double, Double)

计算两个值的按位或两个值。

IBitwiseOperators<Double,Double,Double>.ExclusiveOr(Double, Double)

计算独占值或两个值。

IBitwiseOperators<Double,Double,Double>.OnesComplement(Double)

计算给定值的补补表示形式。

IComparable.CompareTo(Object)

将当前实例与同一类型的另一个对象进行比较,并返回一个整数,该整数指示当前实例在排序顺序中是位于排序顺序中的同一位置、之后还是位于同一位置。

IConvertible.GetTypeCode()

返回 TypeCode 此实例。

IConvertible.ToBoolean(IFormatProvider)

有关此成员的说明,请参阅 ToBoolean(IFormatProvider)

IConvertible.ToByte(IFormatProvider)

有关此成员的说明,请参阅 ToByte(IFormatProvider)

IConvertible.ToChar(IFormatProvider)

不支持此转换。 尝试使用此方法会引发一个 InvalidCastException

IConvertible.ToDateTime(IFormatProvider)

不支持此转换。 尝试使用此方法会引发一个 InvalidCastException

IConvertible.ToDecimal(IFormatProvider)

有关此成员的说明,请参阅 ToDecimal(IFormatProvider)

IConvertible.ToDouble(IFormatProvider)

有关此成员的说明,请参阅 ToDouble(IFormatProvider)

IConvertible.ToInt16(IFormatProvider)

有关此成员的说明,请参阅 ToInt16(IFormatProvider)

IConvertible.ToInt32(IFormatProvider)

有关此成员的说明,请参阅 ToInt32(IFormatProvider)

IConvertible.ToInt64(IFormatProvider)

有关此成员的说明,请参阅 ToInt64(IFormatProvider)

IConvertible.ToSByte(IFormatProvider)

有关此成员的说明,请参阅 ToSByte(IFormatProvider)

IConvertible.ToSingle(IFormatProvider)

有关此成员的说明,请参阅 ToSingle(IFormatProvider)

IConvertible.ToType(Type, IFormatProvider)

有关此成员的说明,请参阅 ToType(Type, IFormatProvider)

IConvertible.ToUInt16(IFormatProvider)

有关此成员的说明,请参阅 ToUInt16(IFormatProvider)

IConvertible.ToUInt32(IFormatProvider)

有关此成员的说明,请参阅 ToUInt32(IFormatProvider)

IConvertible.ToUInt64(IFormatProvider)

有关此成员的说明,请参阅 ToUInt64(IFormatProvider)

IDecrementOperators<Double>.Decrement(Double)

递减值。

IDivisionOperators<Double,Double,Double>.Division(Double, Double)

将一个值除以计算其商。

IFloatingPoint<Double>.GetExponentByteCount()

获取将作为其一部分 TryWriteExponentLittleEndian(Span<Byte>, Int32)写入的字节数。

IFloatingPoint<Double>.GetExponentShortestBitLength()

获取最短两个指数的补数表示形式的长度(以位为单位)。

IFloatingPoint<Double>.GetSignificandBitLength()

获取当前标志的长度(以位为单位)。

IFloatingPoint<Double>.GetSignificandByteCount()

获取将作为其一部分 TryWriteSignificandLittleEndian(Span<Byte>, Int32)写入的字节数。

IFloatingPoint<Double>.TryWriteExponentBigEndian(Span<Byte>, Int32)

尝试将当前指数(采用 big-endian 格式)写入给定范围。

IFloatingPoint<Double>.TryWriteExponentLittleEndian(Span<Byte>, Int32)

尝试将当前指数(以小端格式)写入给定范围。

IFloatingPoint<Double>.TryWriteSignificandBigEndian(Span<Byte>, Int32)

尝试将当前符号(采用 big-endian 格式)写入给定范围。

IFloatingPoint<Double>.TryWriteSignificandLittleEndian(Span<Byte>, Int32)

尝试将当前符号(以小端格式)写入给定范围。

IFloatingPointConstants<Double>.E

获取数学常量 e

IFloatingPointConstants<Double>.Pi

获取数学常量 pi

IFloatingPointConstants<Double>.Tau

获取数学常量 tau

IFloatingPointIeee754<Double>.Epsilon

获取可添加到 0 不会导致 0的最小值。

IFloatingPointIeee754<Double>.NaN

获取一个表示 NaN的值。

IFloatingPointIeee754<Double>.NegativeInfinity

获取一个表示负 infinity值的值。

IFloatingPointIeee754<Double>.NegativeZero

获取一个表示负 zero值的值。

IFloatingPointIeee754<Double>.PositiveInfinity

获取一个表示正 infinity值的值。

IIncrementOperators<Double>.Increment(Double)

递增值。

IMinMaxValue<Double>.MaxValue

获取当前类型的最大值。

IMinMaxValue<Double>.MinValue

获取当前类型的最小值。

IModulusOperators<Double,Double,Double>.Modulus(Double, Double)

将两个值相除以计算其模数或余数。

IMultiplicativeIdentity<Double,Double>.MultiplicativeIdentity

获取当前类型的乘法标识。

IMultiplyOperators<Double,Double,Double>.Multiply(Double, Double)

将两个值相乘以计算其产品。

INumberBase<Double>.IsCanonical(Double)

确定某个值是否在其规范表示形式中。

INumberBase<Double>.IsComplexNumber(Double)

确定值是否表示复数。

INumberBase<Double>.IsImaginaryNumber(Double)

确定值是否表示纯虚数。

INumberBase<Double>.IsZero(Double)

确定值是否为零。

INumberBase<Double>.One

获取类型的值 1

INumberBase<Double>.Radix

获取类型的基数。

INumberBase<Double>.TryConvertFromChecked<TOther>(TOther, Double)

表示双精度浮点数。

INumberBase<Double>.TryConvertFromSaturating<TOther>(TOther, Double)

表示双精度浮点数。

INumberBase<Double>.TryConvertFromTruncating<TOther>(TOther, Double)

表示双精度浮点数。

INumberBase<Double>.TryConvertToChecked<TOther>(Double, TOther)

尝试将当前类型的实例转换为另一种类型,为超出当前类型可表示范围的任何值引发溢出异常。

INumberBase<Double>.TryConvertToSaturating<TOther>(Double, TOther)

尝试将当前类型的实例转换为另一种类型,使超出当前类型的可表示范围的任何值饱和。

INumberBase<Double>.TryConvertToTruncating<TOther>(Double, TOther)

尝试将当前类型的实例转换为另一种类型,截断属于当前类型的可表示范围之外的任何值。

INumberBase<Double>.Zero

获取类型的值 0

ISignedNumber<Double>.NegativeOne

获取类型的值 -1

ISubtractionOperators<Double,Double,Double>.Subtraction(Double, Double)

减去两个值以计算其差异。

IUnaryNegationOperators<Double,Double>.UnaryNegation(Double)

计算值的一元求反。

IUnaryPlusOperators<Double,Double>.UnaryPlus(Double)

计算一元加值。

适用于

线程安全性

此类型的所有成员都是线程安全的。 似乎修改实例状态的成员实际上返回使用新值初始化的新实例。 与任何其他类型一样,读取和写入包含此类型的实例的共享变量必须受到锁的保护,以确保线程安全。

另请参阅