bool TryGetUnit(string unit, out VoltageUnits units) { units = VoltageUnits.V; if (unit.ToLower() == "v") { units = VoltageUnits.V; } else if (unit.ToLower() == "kv") { units = VoltageUnits.kV; } else if (unit.ToLower() == "mv") { units = VoltageUnits.mV; } else if (unit.ToLower() == "nv") { units = VoltageUnits.nV; } else if (unit.ToLower() == "uv") { units = VoltageUnits.μV; } else if (unit.ToLower() == "μv") { units = VoltageUnits.μV; } else { return(false); } return(true); }
public static VoltageValue StrToVoltageValue(string value, out string message) { message = string.Empty; var ss = _doubleRege.Split(value).Where(t => { var s = t.Trim(); return(!(s == "," || s == "" || s == ",")); }); string num = ""; string unit = ""; foreach (var s in ss) { if (_doubleRege.IsMatch(s)) { num = s; continue; } if (_unitRegex.IsMatch(s)) { unit = s; continue; } } string vl = string.IsNullOrEmpty(unit) ? string.Empty : value.Replace(unit, string.Empty); VoltageUnits units = VoltageUnits.V; if (unit.ToLower() == "v") { units = VoltageUnits.V; } else if (unit.ToLower() == "kv") { units = VoltageUnits.kV; } else if (unit.ToLower() == "mv") { units = VoltageUnits.mV; } else if (unit.ToLower() == "nv") { units = VoltageUnits.nV; } else if (unit.ToLower() == "uv") { units = VoltageUnits.μV; } else if (unit.ToLower() == "μv") { units = VoltageUnits.μV; } else { if (double.TryParse(vl, out double d)) { message = $"{unit}不是电压的有效单位,请尝试输入<{vl}kv/v/mv/μv/nv>格式"; return(null); } else { message = $"{unit}不是电压的有效值,请尝试输入<{num}kv/v/mv/μv/nv>格式"; return(null); } } if (vl.EndsWith(".")) { message = $"{vl}值不合法,请尝试输入<{num}{unit}>格式"; return(null); } if (double.TryParse(vl, out double dd)) { return(new VoltageValue(dd, units)); } message = $"{vl}值不合法,请尝试输入<{num}{unit}>格式"; return(null); }
public VoltageValue(double value, VoltageUnits units) { _value = value; _units = units; }