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 值类型表示一个值介于 -1.79769313486232e308 和 +1.79769313486232e308 之间的双精度 64 位数字,以及 +0 或-0、PositiveInfinity、NegativeInfinity 和非数字 (NaN)。
Double 符合二进制浮点算法的 IEC 60559:1989 (IEEE 754) 标准。
Double 为比较此类型的实例,将实例的值转换为它的字符串表示形式以及将数字的字符串表示形式转换为此类型的实例提供了相应的方法。有关格式规范代码如何控制值类型的字符串表示形式的信息,请参见 格式化概述、标准数字格式字符串 和 自定义数字格式字符串。
使用浮点数
在执行二进制运算时,如果其中一个操作数为 Double,那么另一个操作数必须是整数类型或浮点类型(Double 或 Single)。在执行运算之前,如果另一个操作数不是 Double,应将其转换为 Double,并且至少要使用 Double 的范围和精度来执行运算。如果此运算得到一个数字结果,则结果的类型为 Double。
浮点运算符(包括赋值运算符)不会引发异常。在异常情况下,浮点运算的结果为零、无穷或 NaN,如下所述:
如果浮点运算的结果对于目标格式来说太小,则运算的结果为零。
如果浮点运算结果的数值对于目标格式来说太大,则运算的结果为 PositiveInfinity 或 NegativeInfinity(具体取决于结果的符号)。
如果浮点运算无效,则运算的结果为 NaN。
如果浮点运算的一个或两个操作数为 NaN,则运算的结果为 NaN。
请记住,浮点数只能近似于十进制数字,浮点数的精度决定了浮点数近似于十进制数字的精确程度。默认情况下,Double 值的精度是 15 个十进制位,但内部维护的最大精度是 17 位。浮点数的精度有几种结果:
特定精度下看似相等的两个浮点数可能并不相等,因为它们的最小有效位数不同。
由于浮点数可能无法精确近似于十进制数,如果使用十进制数,则使用浮点数的数学或比较运算可能不会产生相同的结果。
如果涉及浮点数,值可能不往返。值的往返是指,某个运算将原始浮点数转换为另一种格式,而反向运算又将转换后的格式转换回浮点数,且最终浮点数与原始浮点数相等。由于一个或多个最低有效位可能在转换中丢失或更改,往返可能会失败。
接口实现
此类型实现接口 IComparable、IComparable、IFormattable 和 IConvertible。使用 Convert 类进行转换,而不是使用此类型的 IConvertible 显式接口成员实现。
示例
下面的代码示例演示 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;
}
}
线程安全
此类型的任何公共静态(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