SIHelper.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6. using System.Threading.Tasks;
  7. namespace Uni_Trend.MSO7000X.Common.Helper
  8. {
  9. public static class SIHelper
  10. {
  11. private static string SICollection = "yzafpnμmDkMGTPEZY";
  12. /// <summary>
  13. /// 按SI单位制为数字添加后缀
  14. /// </summary>
  15. /// <param name="value">需要转换的数字</param>
  16. /// <param name="decimals">小数位数</param>
  17. /// <param name="unit">附加单位</param>
  18. /// <returns></returns>
  19. public static string ValueChangeToSI(double value, int decimals = 1, string unit = "", double hex = 1000, bool invalid = true)
  20. {
  21. if (hex <= 0) hex = 1000;
  22. if (double.IsNaN(value)) return "NaN";
  23. if (value == 0)
  24. {
  25. return value + unit;
  26. }
  27. if (Math.Abs(value) < 1E-24 || Math.Abs(value) > 1E24) return Math.Round((decimal)0f, decimals) + unit;
  28. string SI = SICollection;
  29. double d = Math.Log(Math.Abs(value), hex);
  30. decimal number = Math.Round((decimal)(value / Math.Pow(hex, Math.Floor(d))), decimals);
  31. d += 8;
  32. string s = SI.Substring((int)d, 1);
  33. if (s == "D") s = "";
  34. if (!invalid)
  35. {
  36. string formatstring = "#0";
  37. if (decimals > 0)
  38. {
  39. formatstring += ".";
  40. for (int i = 0; i < decimals; i++) formatstring += "#";
  41. }
  42. return number.ToString(formatstring) + s + unit;
  43. }
  44. else
  45. {
  46. string formatstr = "{0:N" + decimals + "}{1}{2}";
  47. return string.Format(formatstr, number, s, unit);
  48. }
  49. }
  50. public static string ValueChangeToSI(double value, out string siUnit, out string numberstring, int decimals = 1, string unit = "", bool invalid = false)
  51. {
  52. string formatstring = "#0";
  53. if (!invalid)
  54. {
  55. if (decimals > 0)
  56. {
  57. formatstring += ".";
  58. for (int i = 0; i < decimals; i++) formatstring += "#";
  59. }
  60. }
  61. else
  62. {
  63. formatstring = "N" + decimals;
  64. }
  65. siUnit = unit;
  66. if (double.IsNaN(value))
  67. {
  68. numberstring = "NaN";
  69. return "NaN";
  70. }
  71. if (value == 0)
  72. {
  73. numberstring = value.ToString(formatstring);
  74. return numberstring + unit;
  75. }
  76. if (Math.Abs(value) < 1E-24)
  77. {
  78. numberstring = Math.Round((decimal)0f, decimals) + "";
  79. return Math.Round((decimal)0f, decimals) + unit;
  80. }
  81. string SI = SICollection;
  82. double d = Math.Log(Math.Abs(value), 1000);
  83. decimal number = Math.Round((decimal)(value / Math.Pow(1000, Math.Floor(d))), decimals);
  84. d += 8;
  85. if (d > 16)
  86. {
  87. numberstring = "NaN";
  88. siUnit = "";
  89. return "";
  90. }
  91. string s = SI.Substring((int)d, 1);
  92. if (s == "D") s = "";
  93. siUnit = s + unit;
  94. numberstring = number.ToString(formatstring);
  95. return numberstring + siUnit;
  96. }
  97. public static double SIChangeToValue(string SI, string unit = "", double hex = 1000)
  98. {
  99. try
  100. {
  101. if (hex <= 0) hex = 1000;
  102. if (string.IsNullOrEmpty(SI)) return 0;
  103. string s = Regex.Replace(SI, unit + "$", "").Trim();
  104. string SIunit = s.Substring(s.Length - 1, 1);
  105. double Value = 0;
  106. if (int.TryParse(SIunit, out int Number))
  107. {
  108. double.TryParse(s, out Value);
  109. }
  110. else
  111. {
  112. string units = SICollection;
  113. int index = units.IndexOf(SIunit) - 8;
  114. double d = Math.Pow(hex, index);
  115. double.TryParse(s.Substring(0, s.Length - 1), out Value);
  116. Value *= d;
  117. }
  118. return Value;
  119. }
  120. catch { return 0; }
  121. }
  122. public static string FormatString(string str)
  123. {
  124. string[] s = str.Split('.');
  125. if (s.Length == 0) return str;
  126. string s0 = s[0];
  127. if (s0.Length > 3)
  128. {
  129. List<string> ss = new List<string>();
  130. ss.Add(s0.Substring(0, s0.Length - ((int)s0.Length / 3) * 3));
  131. s0 = s0.Substring(ss[0].Length);
  132. for (int i = 0; i < Math.Floor(s0.Length / 3f); i++)
  133. {
  134. ss.Add(s0.Substring(i * 3, 3));
  135. }
  136. s0 = string.Join(" ", ss.ToArray()).Trim();
  137. }
  138. if (s.Length == 1) return s0;
  139. string s1 = s[1];
  140. if (s1.Length > 3)
  141. {
  142. List<string> ss = new List<string>();
  143. for (int i = 0; i < Math.Floor(s1.Length / 3f); i++)
  144. {
  145. ss.Add(s1.Substring(i * 3, 3));
  146. }
  147. ss.Add(s1.Substring(((int)(s1.Length / 3)) * 3, s1.Length - ((int)(s1.Length / 3)) * 3));
  148. s1 = string.Join(" ", ss.ToArray()).Trim();
  149. }
  150. return string.Join(".", new string[] { s0, s1 });
  151. }
  152. /// <summary>
  153. /// SI单位的互相转换
  154. /// </summary>
  155. /// <param name="sourceValue"></param>
  156. /// <param name="sourceUnitIndex"></param>
  157. /// <param name="destUnitIndex"></param>
  158. /// <param name="hex"></param>
  159. /// <returns></returns>
  160. public static double SIUnitConversion(Double sourceValue, Int32 sourceUnitIndex, Int32 destUnitIndex, double hex = 1000)
  161. {
  162. if (hex <= 0) hex = 1000;
  163. Int32 index = sourceUnitIndex - destUnitIndex;
  164. return sourceValue * Math.Pow(hex, index);
  165. }
  166. }
  167. }