using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; namespace Uni_Trend.MSO7000X.Common.Helper { public static class SIHelper { private static string SICollection = "yzafpnμmDkMGTPEZY"; /// /// 按SI单位制为数字添加后缀 /// /// 需要转换的数字 /// 小数位数 /// 附加单位 /// public static string ValueChangeToSI(double value, int decimals = 1, string unit = "", double hex = 1000, bool invalid = true) { if (hex <= 0) hex = 1000; if (double.IsNaN(value)) return "NaN"; if (value == 0) { return value + unit; } if (Math.Abs(value) < 1E-24 || Math.Abs(value) > 1E24) return Math.Round((decimal)0f, decimals) + unit; string SI = SICollection; double d = Math.Log(Math.Abs(value), hex); decimal number = Math.Round((decimal)(value / Math.Pow(hex, Math.Floor(d))), decimals); d += 8; string s = SI.Substring((int)d, 1); if (s == "D") s = ""; if (!invalid) { string formatstring = "#0"; if (decimals > 0) { formatstring += "."; for (int i = 0; i < decimals; i++) formatstring += "#"; } return number.ToString(formatstring) + s + unit; } else { string formatstr = "{0:N" + decimals + "}{1}{2}"; return string.Format(formatstr, number, s, unit); } } public static string ValueChangeToSI(double value, out string siUnit, out string numberstring, int decimals = 1, string unit = "", bool invalid = false) { string formatstring = "#0"; if (!invalid) { if (decimals > 0) { formatstring += "."; for (int i = 0; i < decimals; i++) formatstring += "#"; } } else { formatstring = "N" + decimals; } siUnit = unit; if (double.IsNaN(value)) { numberstring = "NaN"; return "NaN"; } if (value == 0) { numberstring = value.ToString(formatstring); return numberstring + unit; } if (Math.Abs(value) < 1E-24) { numberstring = Math.Round((decimal)0f, decimals) + ""; return Math.Round((decimal)0f, decimals) + unit; } string SI = SICollection; double d = Math.Log(Math.Abs(value), 1000); decimal number = Math.Round((decimal)(value / Math.Pow(1000, Math.Floor(d))), decimals); d += 8; if (d > 16) { numberstring = "NaN"; siUnit = ""; return ""; } string s = SI.Substring((int)d, 1); if (s == "D") s = ""; siUnit = s + unit; numberstring = number.ToString(formatstring); return numberstring + siUnit; } public static double SIChangeToValue(string SI, string unit = "", double hex = 1000) { try { if (hex <= 0) hex = 1000; if (string.IsNullOrEmpty(SI)) return 0; string s = Regex.Replace(SI, unit + "$", "").Trim(); string SIunit = s.Substring(s.Length - 1, 1); double Value = 0; if (int.TryParse(SIunit, out int Number)) { double.TryParse(s, out Value); } else { string units = SICollection; int index = units.IndexOf(SIunit) - 8; double d = Math.Pow(hex, index); double.TryParse(s.Substring(0, s.Length - 1), out Value); Value *= d; } return Value; } catch { return 0; } } public static string FormatString(string str) { string[] s = str.Split('.'); if (s.Length == 0) return str; string s0 = s[0]; if (s0.Length > 3) { List ss = new List(); ss.Add(s0.Substring(0, s0.Length - ((int)s0.Length / 3) * 3)); s0 = s0.Substring(ss[0].Length); for (int i = 0; i < Math.Floor(s0.Length / 3f); i++) { ss.Add(s0.Substring(i * 3, 3)); } s0 = string.Join(" ", ss.ToArray()).Trim(); } if (s.Length == 1) return s0; string s1 = s[1]; if (s1.Length > 3) { List ss = new List(); for (int i = 0; i < Math.Floor(s1.Length / 3f); i++) { ss.Add(s1.Substring(i * 3, 3)); } ss.Add(s1.Substring(((int)(s1.Length / 3)) * 3, s1.Length - ((int)(s1.Length / 3)) * 3)); s1 = string.Join(" ", ss.ToArray()).Trim(); } return string.Join(".", new string[] { s0, s1 }); } /// /// SI单位的互相转换 /// /// /// /// /// /// public static double SIUnitConversion(Double sourceValue, Int32 sourceUnitIndex, Int32 destUnitIndex, double hex = 1000) { if (hex <= 0) hex = 1000; Int32 index = sourceUnitIndex - destUnitIndex; return sourceValue * Math.Pow(hex, index); } } }