/// <summary> /// Returns a string representation with SI unit included with the given prefix. Alternative number complex number string /// representation is given as a sum instead of a coordinate on the complex plane, eg: (24+5i)kV /// </summary> /// <param name="value"></param> /// <param name="unit"></param> /// <param name="prefix"></param> /// <param name="useFullName"></param> /// <param name="imaginaryAsJ">If true, "j" is used as imaginary unit instead of "i"</param> /// <returns></returns> public static string ToAltSIString(Complex value, SIPrefix prefix, string unit = "", int roundToDigit = 0, MidpointRounding midpointRounding = MidpointRounding.AwayFromZero, bool useFullName = false, bool imaginaryAsJ = false) { // Modify the value to match it value /= Math.Pow(10, prefix.Base10Power); // If roundToDigit is greater than 0, round the value to a specific digit if (roundToDigit > 0) { value = value.RoundToDigit(roundToDigit, midpointRounding); } // If value is 0 return "0" plus the unit if (value == 0) { return("0" + unit); } string result = string.Empty; // If real part is not 0 if (value.Real != 0) { // Add its string version to the result result += value.Real.ToString(); } // If imaginary is not 0 if (value.Imaginary != 0) { // And it's nonnegative and the real part wasn't 0 if (value.Imaginary >= 0 && value.Real != 0) { // Include a plus sign result += "+"; } // Add the imaginary part as string to the result with the imaginary unit result += value.Imaginary.ToString() + (imaginaryAsJ ? "j" : "i"); // Wrap the whole thing in brackets result = "(" + result + ")"; } // Finish it off with unit + prefix result += (useFullName ? prefix.Name : prefix.Symbol) + unit; return(result); }
/// <summary> /// Returns a string representation with SI unit included with the given prefix /// </summary> /// <param name="value"></param> /// <param name="unit"></param> /// <param name="prefix"></param> /// <param name="useFullName"></param> /// <returns></returns> public static string ToSIString(Complex value, SIPrefix prefix, string unit = "", int roundToDigit = 0, MidpointRounding midpointRounding = MidpointRounding.AwayFromZero, bool useFullName = false) { // Modify the value to match it value /= Math.Pow(10, prefix.Base10Power); // If roundToDigit is greater than 0, round the value to a specific digit if (roundToDigit > 0) { value = value.RoundToDigit(roundToDigit, midpointRounding); } // Return it plus prefix name (or symbol) and unit return(value.ToString() + (useFullName ? prefix.Name : prefix.Symbol) + unit); }
/// <summary> /// Converts a number without prefix to a value usable with <paramref name="prefix"/> /// </summary> /// <param name="prefix"></param> /// <param name="value"></param> /// <returns></returns> public static double Convert(this SIPrefix prefix, double value) => value *Math.Pow(10, prefix.Base10Power);