/// <summary> /// 将文本转化为物理量 /// </summary> /// <param name="text">代表物理量的文本</param> /// <returns>转化后的物理量</returns> public static PhysicalVariable Text2Value(string text) { bool successed; PhysicalVariable rtn = Text2Value(text, out successed); return(rtn); }
/// <summary> /// 将数值转换自动转换为SI词头的有效位数表示方式 /// </summary> /// <param name="value">需要转换的物理量</param> /// <returns>带有SI词头的有效位数表示方式文本</returns> /// <param name="percentage">百分号显示</param> /// <param name="positiveSign">正值显示+号</param> /// <returns>带有SI词头的有效位数表示方式文本</returns> public static string Value2Text(PhysicalVariable value, bool percentage = false, bool positiveSign = false, bool usePrefix = true) { if (value.EffectiveLength > 0) { return(Value2Text(value, (int)value.EffectiveLength, DefaultConstant.NoiseLevel(value.PhysicalVariableType), percentage, positiveSign, usePrefix)); } else { return(Value2Text(value, DefaultConstant.EffectiveLength(value.PhysicalVariableType), DefaultConstant.NoiseLevel(value.PhysicalVariableType), percentage, positiveSign, usePrefix)); } }
/// <summary> /// 将数值转换自动转换为SI词头的有效位数表示方式 /// </summary> /// <param name="value">需要转换的物理量</param> /// <param name="effectiveLength">有效位数长度(必须是正整数)</param> /// <param name="noiseLevel">物理量底噪电平</param> /// <param name="percentage">百分号显示</param> /// <param name="positiveSign">正值显示+号</param> /// <returns>带有SI词头的有效位数表示方式文本</returns> public static string Value2Text(PhysicalVariable value, int effectiveLength, int noiseLevel, bool percentage = false, bool positiveSign = false, bool usePrefix = true) { if (value == null) { return(string.Empty); } if (value.value != null) { return(Value2Text((double)value.value, effectiveLength, noiseLevel, string.Empty, value.PhysicalVariableType, percentage, positiveSign, usePrefix)); } else { return(value.OriginText); } }
/// <summary> /// 将文本转化为物理量 /// </summary> /// <param name="text">代表物理量的文本</param> /// <param name="successed">是否转换成功</param> /// <returns>转化后的物理量(当successed = true时有效)</returns> public static PhysicalVariable Text2Value(string text, out bool successed) { PhysicalVariable rtn = new PhysicalVariable(); string num = string.Empty; string suffix = string.Empty; int i = 0; foreach (char c in text) { if (c != ' ') { if (c != '?') { if (c != '\0') { if (c != ';') { num += c; } } } } } rtn.OriginText = num; if (text.Length < 1) { successed = false; return(rtn); } if (num.StartsWith("+")) { num = num.Remove(0, 1); } while ((num.Length > 0) && (!Microsoft.VisualBasic.Information.IsNumeric(num))) { suffix = num[num.Length - 1] + suffix; num = num.Remove(num.Length - 1, 1); } if (num == string.Empty) { successed = false; rtn.PhysicalVariableType = QuantityName.None; return(rtn); } suffix = suffix.Trim(); Symbol symbol = QuantitiesConverter.String2Symbol(suffix); switch (symbol) { case Symbol.None: break; case Symbol.VAR: suffix = suffix.Remove(suffix.Length - 3); break; case Symbol.Hz: case Symbol.VA: suffix = suffix.Remove(suffix.Length - 2); break; default: suffix = suffix.Remove(suffix.Length - 1); break; } bool percentage = suffix.StartsWith("%"); if (percentage) { suffix = suffix.Remove(0, 1); } if (PrefixsConverter.isPrefix(suffix) == false) { successed = false; rtn.PhysicalVariableType = QuantityName.None; return(rtn); } rtn.value = double.Parse(num); rtn.value *= Math.Pow(10, PrefixsConverter.prefixString2dec(suffix)); if (percentage) { rtn.value /= 100.0; } rtn.EffectiveLength = 0; i = 0; bool notZero = false; while (i < num.Length) { if (num[i] == '.') { i++; } if (i >= num.Length) { successed = false; rtn.PhysicalVariableType = QuantityName.None; return(rtn); } if (num[i] == '0') { if (notZero) { rtn.EffectiveLength++; i++; } else { i++; } } else if ((num[i] >= '1') && (num[i] <= '9')) { notZero = true; rtn.EffectiveLength++; i++; } else { i = num.Length; } } rtn.PhysicalVariableType = (QuantityName)symbol; successed = true; return(rtn); }
/// <summary> /// 将数值转换自动转换为SI词头的有效位数表示方式 /// </summary> /// <param name="value">需要转换的物理量</param> /// <param name="effectiveLength">有效位数长度(必须是正整数)</param> /// <param name="percentage">百分号显示</param> /// <param name="positiveSign">正值显示+号</param> /// <returns>带有SI词头的有效位数表示方式文本</returns> public static string Value2Text(PhysicalVariable value, int effectiveLength, bool percentage = false, bool positiveSign = false) { return(Value2Text(value, effectiveLength, DefaultConstant.NoiseLevel(value.PhysicalVariableType), percentage, positiveSign)); }