C#中比较浮点类型大小的方法(double float)
最近工作需要处理double类型数据,写了些浮点类型比较大小的方法,供参考。
1 ///<summary>
2 /// 处理浮点型比较大小的方法
3 ///</summary>
4 internal class DoubleUtil
5 {
6 // Fields 浮点型的误差
7 private const double DOUBLE_DELTA = 1E-06;
8
9 public static bool AreEqual(double value1, double value2)
10 {
11 return (value1 == value2)
12 || Math.Abs(value1 - value2) < DOUBLE_DELTA;
13 }
14
15 public static bool GreaterThan(double value1, double value2)
16 {
17 return ((value1 > value2) && !AreEqual(value1, value2));
18 }
19
20 public static bool GreaterThanOrEqual(double value1, double value2)
21 {
22 return (value1 > value2) || AreEqual(value1, value2);
23 }
24
25 public static bool IsZero(double value)
26 {
27 return (Math.Abs(value) < DOUBLE_DELTA);
28 }
29
30 public static bool LessThan(double value1, double value2)
31 {
32 return ((value1 < value2) && !AreEqual(value1, value2));
33 }
34
35 public static bool LessThanOrEqual(double value1, double value2)
36 {
37 return (value1 < value2) || AreEqual(value1, value2);
38 }
39 }