SystemExtension.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace System
  7. {
  8. public static class SystemExtension
  9. {
  10. public static string ToSingleString(this IEnumerable<string> strList, char seperator = ';')
  11. {
  12. if (strList == null || strList.Count() == 0) return string.Empty;
  13. bool isFirst = true;
  14. string strRet = string.Empty;
  15. foreach (string str in strList)
  16. {
  17. if (isFirst)
  18. {
  19. strRet += string.IsNullOrEmpty(str) ? string.Empty : str;
  20. isFirst = false;
  21. continue;
  22. }
  23. strRet += seperator + (string.IsNullOrEmpty(str) ? string.Empty : str);
  24. }
  25. return strRet;
  26. }
  27. #region double
  28. public static bool AreEqual(this double value1, double value2, double torrence = 1E-6)
  29. {
  30. return (value1 == value2)
  31. || Math.Abs(value1 - value2) < torrence;
  32. }
  33. public static bool GreaterThan(this double value1, double value2, double torrence = 1E-6)
  34. {
  35. return ((value1 > value2) && !AreEqual(value1, value2, torrence));
  36. }
  37. public static bool GreaterThanOrEqual(this double value1, double value2, double torrence = 1E-6)
  38. {
  39. return (value1 > value2) || AreEqual(value1, value2, torrence);
  40. }
  41. public static bool IsZero(this double value, double torrence = 1E-6)
  42. {
  43. return (Math.Abs(value) < torrence);
  44. }
  45. public static bool LessThan(this double value1, double value2, double torrence = 1E-6)
  46. {
  47. return ((value1 < value2) && !AreEqual(value1, value2, torrence));
  48. }
  49. public static bool LessThanOrEqual(this double value1, double value2, double torrence = 1E-6)
  50. {
  51. return (value1 < value2) || AreEqual(value1, value2, torrence);
  52. }
  53. /// <summary>
  54. /// 四舍五入
  55. /// </summary>
  56. /// <param name="target"></param>
  57. /// <param name="multiple">... 0.01,0.1,1,10,100,1000 ...</param>
  58. /// <returns></returns>
  59. public static double HalfAdjust(this double target, double multiple = 1)
  60. {
  61. double temp = target / multiple;
  62. double intPart = temp < 0 ? Math.Ceiling(temp) : Math.Floor(temp);
  63. double decimalPart = Math.Abs(temp - intPart);
  64. if (decimalPart >= 0.5)
  65. {
  66. if (temp < 0)
  67. intPart--;
  68. else intPart++;
  69. }
  70. return intPart * multiple;
  71. }
  72. #endregion double
  73. #region float
  74. public static bool AreEqual(this float value1, float value2, double torrence = 1E-6)
  75. {
  76. return (value1 == value2)
  77. || Math.Abs(value1 - value2) < torrence;
  78. }
  79. public static bool GreaterThan(this float value1, float value2, double torrence = 1E-6)
  80. {
  81. return ((value1 > value2) && !AreEqual(value1, value2, torrence));
  82. }
  83. public static bool GreaterThanOrEqual(this float value1, float value2, double torrence = 1E-6)
  84. {
  85. return (value1 > value2) || AreEqual(value1, value2, torrence);
  86. }
  87. public static bool IsZero(this float value, double torrence = 1E-6)
  88. {
  89. return (Math.Abs(value) < torrence);
  90. }
  91. public static bool LessThan(this float value1, float value2, double torrence = 1E-6)
  92. {
  93. return ((value1 < value2) && !AreEqual(value1, value2, torrence));
  94. }
  95. public static bool LessThanOrEqual(this float value1, float value2, double torrence = 1E-6)
  96. {
  97. return (value1 < value2) || AreEqual(value1, value2, torrence);
  98. }
  99. /// <summary>
  100. /// 四舍五入
  101. /// </summary>
  102. /// <param name="target"></param>
  103. /// <param name="multiple">... 0.01,0.1,1,10,100,1000 ...</param>
  104. /// <returns></returns>
  105. public static float HalfAdjust(this float target, float multiple = 1)
  106. {
  107. float temp = target / multiple;
  108. float intPart = temp < 0 ? (float)Math.Ceiling(temp) : (float)Math.Floor(temp);
  109. float decimalPart = Math.Abs(temp - intPart);
  110. if (decimalPart >= 0.5)
  111. {
  112. if (temp < 0)
  113. intPart--;
  114. else intPart++;
  115. }
  116. return intPart * multiple;
  117. }
  118. #endregion float
  119. }
  120. }