当前位置: 首页>>代码示例>>C#>>正文


C# Double结构体代码示例

本文整理汇总了C#中System.Double结构体的典型用法代码示例。如果您正苦于以下问题:C# Double结构体的具体用法?C# Double怎么用?C# Double使用的例子?那么恭喜您, 这里精选的结构体代码示例或许可以为您提供帮助。


Double结构体属于System命名空间,在下文中一共展示了Double结构体的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: CompareTo

// 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;
        }
    }
}
开发者ID:.NET开发者,项目名称:System,代码行数:69,代码来源:Double

示例2: Main

//引入命名空间
using System;

public class Example
{
   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:           {0:R}", result1);
      Console.WriteLine(".1 Added 10 times: {0:R}", result2);
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:17,代码来源:Double

输出:

.1 * 10:           1
.1 Added 10 times: 0.99999999999999989

示例3: Main

//引入命名空间
using System;

public class Example
{
   public static void Main()
   {
      Double value = 123456789012.34567;
      Double additional = Double.Epsilon * 1e15;
      Console.WriteLine("{0} + {1} = {2}", value, additional, 
                                           value + additional);
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:13,代码来源:Double

输出:

123456789012.346 + 4.94065645841247E-309 = 123456789012.346

示例4: Main

//引入命名空间
using System;

public class Example
{
   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 ({0}) does not equal the total ({1}).",
                           total, result); 
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:20,代码来源:Double

输出:

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

示例5: Main

//引入命名空间
using System;
using System.IO;

public class Example
{
   public static void Main()
   {
      StreamWriter sw = new StreamWriter(@".\Doubles.dat");
      Double[] values = { 2.2/1.01, 1.0/3, Math.PI };
      for (int ctr = 0; ctr < values.Length; ctr++) {
         sw.Write(values[ctr].ToString());
         if (ctr != values.Length - 1)
            sw.Write("|");
      }      
      sw.Close();
      
      Double[] restoredValues = new Double[values.Length];
      StreamReader sr = new StreamReader(@".\Doubles.dat");
      string temp = sr.ReadToEnd();
      string[] tempStrings = temp.Split('|');
      for (int ctr = 0; ctr < tempStrings.Length; ctr++)
         restoredValues[ctr] = Double.Parse(tempStrings[ctr]);   

      for (int ctr = 0; ctr < values.Length; ctr++)
         Console.WriteLine("{0} {2} {1}", values[ctr], 
                           restoredValues[ctr],
                           values[ctr].Equals(restoredValues[ctr]) ? "=" : "<>");
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:30,代码来源:Double

输出:

2.17821782178218 <> 2.17821782178218
0.333333333333333 <> 0.333333333333333
3.14159265358979 <> 3.14159265358979

示例6: Main

//引入命名空间
using System;
using System.IO;

public class Example
{
   public static void Main()
   {
      StreamWriter sw = new StreamWriter(@".\Doubles.dat");
      Double[] values = { 2.2/1.01, 1.0/3, Math.PI };
      for (int ctr = 0; ctr < values.Length; ctr++) 
         sw.Write("{0:G17}{1}", values[ctr], ctr < values.Length - 1 ? "|" : "" );

      sw.Close();
      
      Double[] restoredValues = new Double[values.Length];
      StreamReader sr = new StreamReader(@".\Doubles.dat");
      string temp = sr.ReadToEnd();
      string[] tempStrings = temp.Split('|');
      for (int ctr = 0; ctr < tempStrings.Length; ctr++)
         restoredValues[ctr] = Double.Parse(tempStrings[ctr]);   

      for (int ctr = 0; ctr < values.Length; ctr++)
         Console.WriteLine("{0} {2} {1}", values[ctr], 
                           restoredValues[ctr],
                           values[ctr].Equals(restoredValues[ctr]) ? "=" : "<>");
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:28,代码来源:Double

输出:

2.17821782178218 = 2.17821782178218
0.333333333333333 = 0.333333333333333
3.14159265358979 = 3.14159265358979

示例7: Main

//引入命名空间
using System;

public class Example
{
   public static void Main()
   {
      Double value1 = 1/3.0;
      Single sValue2 = 1/3.0f;
      Double value2 = (Double) sValue2;
      Console.WriteLine("{0:R} = {1:R}: {2}", value1, value2, 
                                          value1.Equals(value2));
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:14,代码来源:Double

输出:

0.33333333333333331 = 0.3333333432674408: False

示例8:

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);      
// 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
开发者ID:.NET开发者,项目名称:System,代码行数:18,代码来源:Double

示例9: Main

//引入命名空间
using System;

public class Example
{
   public static void Main()
   {
      double value1 = .333333333333333;
      double value2 = 1.0/3;
      Console.WriteLine("{0:R} = {1:R}: {2}", value1, value2, value1.Equals(value2));
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:12,代码来源:Double

输出:

0.333333333333333 = 0.33333333333333331: False

示例10: Main

//引入命名空间
using System;

public class Example
{
   public static void Main()
   {
      double value1 = 100.10142;
      value1 = Math.Sqrt(Math.Pow(value1, 2));
      double value2 = Math.Pow(value1 * 3.51, 2);
      value2 = Math.Sqrt(value2) / 3.51;
      Console.WriteLine("{0} = {1}: {2}\n", 
                        value1, value2, value1.Equals(value2)); 
      Console.WriteLine("{0:R} = {1:R}", value1, value2); 
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:16,代码来源:Double

输出:

100.10142 = 100.10142: False

100.10142 = 100.10141999999999

示例11: Main

//引入命名空间
using System;

public class Example
{
   public static void Main()
   {
      double value1 = .333333333333333;
      double value2 = 1.0/3;
      int precision = 7;
      value1 = Math.Round(value1, precision);
      value2 = Math.Round(value2, precision);
      Console.WriteLine("{0:R} = {1:R}: {2}", value1, value2, value1.Equals(value2));
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:15,代码来源:Double

输出:

0.3333333 = 0.3333333: True

示例12: Main

//引入命名空间
using System;

public class Example
{
   public static void Main()
   {
      double one1 = .1 * 10;
      double one2 = 0;
      for (int ctr = 1; ctr <= 10; ctr++)
         one2 += .1;

      Console.WriteLine("{0:R} = {1:R}: {2}", one1, one2, one1.Equals(one2));
      Console.WriteLine("{0:R} is approximately equal to {1:R}: {2}", 
                        one1, 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;           
   } 
}
开发者ID:.NET开发者,项目名称:System,代码行数:38,代码来源:Double

输出:

1 = 0.99999999999999989: False
1 is approximately equal to 0.99999999999999989: True

示例13: Main

//引入命名空间
using System;

public class Example
{
   public static void Main()
   {
      Double value1 = 1.1632875981534209e-225;
      Double value2 = 9.1642346778e-175;
      Double result = value1 * value2;
      Console.WriteLine("{0} * {1} = {2}", value1, value2, result);
      Console.WriteLine("{0} = 0: {1}", result, result.Equals(0.0));
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:14,代码来源:Double

输出:

1.16328759815342E-225 * 9.1642346778E-175 = 0
0 = 0: True

示例14: Main

//引入命名空间
using System;

public class Example
{
   public static void Main()
   {
      Double value1 = 4.565e153;
      Double value2 = 6.9375e172;
      Double result = value1 * value2;
      Console.WriteLine("PositiveInfinity: {0}", 
                         Double.IsPositiveInfinity(result));
      Console.WriteLine("NegativeInfinity: {0}\n", 
                        Double.IsNegativeInfinity(result));

      value1 = -value1;
      result = value1 * value2;
      Console.WriteLine("PositiveInfinity: {0}", 
                         Double.IsPositiveInfinity(result));
      Console.WriteLine("NegativeInfinity: {0}", 
                        Double.IsNegativeInfinity(result));
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:23,代码来源:Double

输出:

PositiveInfinity: True
NegativeInfinity: False

PositiveInfinity: False
NegativeInfinity: True

示例15: Main

//引入命名空间
using System;

public class Example
{
   public static void Main()
   {
      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 (var value in values) {
         if (value.GetType() == typeof(Decimal))
            dblValue = (Double) value;
         else
            dblValue = value;
         Console.WriteLine("{0} ({1}) --> {2:R} ({3})",
                           value, value.GetType().Name,
                           dblValue, dblValue.GetType().Name);
      }
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:26,代码来源:Double

输出:

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)


注:本文中的System.Double结构体示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。