public static string FormatSI(double number, SIUnitType type) { // Return a spacing string if number is 0; if (number == 0) { return "-----"; } // Assign memory for storing the notations. string[] notation = { "" }; // Select the SIUnitType used and populate the notation array. switch (type) { case SIUnitType.Distance: return ToDistance(number); // Quick and dirty implementation of the new distance formatter from KER 1.0 //notation = new string[] { "mm", "m", "km", "Mm", "Gm", "Tm", "Pm", "Em", "Zm", "Ym" }; //number *= 1000; case SIUnitType.Speed: notation = new string[] { "mm/s", "m/s", "km/s", "Mm/s", "Gm/s", "Tm/s", "Pm/s", "Em/s", "Zm/s", "Ym/s" }; number *= 1000; break; case SIUnitType.Pressure: notation = new string[] { "Pa", "kPa", "MPa", "GPa", "TPa", "PPa", "EPa", "ZPa", "YPa" }; number *= 1000; break; case SIUnitType.Density: notation = new string[] { "mg/m³", "g/m³", "kg/m³", "Mg/m³", "Gg/m³", "Tg/m³", "Pg/m³", "Eg/m³", "Zg/m³", "Yg/m³" }; number *= 1000000; break; case SIUnitType.Force: notation = new string[] { "N", "kN", "MN", "GN", "TN", "PT", "EN", "ZN", "YN" }; number *= 1000; break; case SIUnitType.Mass: notation = new string[] { "g", "kg", "Mg", "Gg", "Tg", "Pg", "Eg", "Zg", "Yg" }; number *= 1000; break; } int notationIndex = 0; // Index that is used to select the notation to display. // Loop through the notations until the smallest usable one is found. for (notationIndex = 0; notationIndex < notation.Length; notationIndex++) { // If the number is now in a sensible range then return a string of the concatenated number and selected notation. if (number <= 1000d && number >= -1000d) return number.ToString("0.000") + notation[notationIndex]; // Switch to bigger unit number /= 1000; } // If we fall out of the loop then we can't display return "-OVF-"; }
public void SetUnitScale(SIUnitType unit, SciNumber scale) { var distanceScaleInfo = unitScales.FirstOrDefault(x => x.unit == unit); if (distanceScaleInfo != null) { distanceScaleInfo.scale = scale; } else { unitScales.Add(new UnitScale() { scale = scale, unit = unit }); } }
public int ExponentFor(SIUnitType unit) { return(unitExponents.FirstOrDefault(x => x.unit == unit)?.exponent ?? 0); }
public Units(SIUnitType unit) : this(new UnitExponent(unit)) { }
public UnitExponent(SIUnitType unit, int exponent = 1) { this.unit = unit; this.exponent = exponent; }
public static string FormatSI(double number, SIUnitType type) { // Return a spacing string if number is 0; if (number == 0) { return("-----"); } // Assign memory for storing the notations. string[] notation = { "" }; // Select the SIUnitType used and populate the notation array. switch (type) { case SIUnitType.Distance: return(ToDistance(number)); // Quick and dirty implementation of the new distance formatter from KER 1.0 //notation = new string[] { "mm", "m", "km", "Mm", "Gm", "Tm", "Pm", "Em", "Zm", "Ym" }; //number *= 1000; case SIUnitType.Speed: notation = new string[] { "mm/s", "m/s", "km/s", "Mm/s", "Gm/s", "Tm/s", "Pm/s", "Em/s", "Zm/s", "Ym/s" }; number *= 1000; break; case SIUnitType.Pressure: notation = new string[] { "Pa", "kPa", "MPa", "GPa", "TPa", "PPa", "EPa", "ZPa", "YPa" }; number *= 1000; break; case SIUnitType.Density: notation = new string[] { "mg/m³", "g/m³", "kg/m³", "Mg/m³", "Gg/m³", "Tg/m³", "Pg/m³", "Eg/m³", "Zg/m³", "Yg/m³" }; number *= 1000000; break; case SIUnitType.Force: notation = new string[] { "N", "kN", "MN", "GN", "TN", "PT", "EN", "ZN", "YN" }; number *= 1000; break; case SIUnitType.Mass: notation = new string[] { "g", "kg", "Mg", "Gg", "Tg", "Pg", "Eg", "Zg", "Yg" }; number *= 1000; break; } int notationIndex = 0; // Index that is used to select the notation to display. // Loop through the notations until the smallest usable one is found. for (notationIndex = 0; notationIndex < notation.Length; notationIndex++) { if (number > 1000 || number < -1000) { number /= 1000; } else { break; } } // Return a string of the concatinated number and selected notation. return(number.ToString("0.000") + notation[notationIndex]); }