/// <summary>Convert this quantity to the target UOM</summary> /// /// <param name="toUOM">UnitOfMeasure</param> /// /// <returns>Converted quantity</returns> /// public Quantity Convert(UnitOfMeasure toUOM) { double multiplier = UOM.GetConversionFactor(toUOM); double thisOffset = UOM.Offset; double targetOffset = toUOM.Offset; // adjust for a non-zero "this" offset double offsetAmount = Amount + thisOffset; // new path amount double newAmount = offsetAmount * multiplier; // adjust for non-zero target offset newAmount = newAmount - targetOffset; // create the quantity now return(new Quantity(newAmount, toUOM)); }
internal PathParameters(UnitOfMeasure pathUOM, double pathFactor) { PathUOM = pathUOM; PathFactor = pathFactor; }
// compose the base symbol internal String BuildBaseString() { StringBuilder numerator = new StringBuilder(); StringBuilder denominator = new StringBuilder(); int numeratorCount = 0; int denominatorCount = 0; // sort units by symbol (ascending) SortedDictionary <UnitOfMeasure, int> keys = new SortedDictionary <UnitOfMeasure, int>(Terms); foreach (KeyValuePair <UnitOfMeasure, int> pair in keys) { UnitOfMeasure unit = pair.Key; int power = pair.Value; if (power < 0) { // negative, put in denominator if (denominator.Length > 0) { denominator.Append(MULT); } if (!unit.Equals(MeasurementSystem.GetSystem().GetOne())) { denominator.Append(unit.Symbol); denominatorCount++; } if (power < -1) { if (power == -2) { denominator.Append(SQ); } else if (power == -3) { denominator.Append(CUBED); } else { denominator.Append(POW).Append(Math.Abs(power)); } } } else if (power >= 1 && !unit.Equals(MeasurementSystem.GetSystem().GetOne())) { // positive, put in numerator if (numerator.Length > 0) { numerator.Append(MULT); } numerator.Append(unit.Symbol); numeratorCount++; if (power > 1) { if (power == 2) { numerator.Append(SQ); } else if (power == 3) { numerator.Append(CUBED); } else { numerator.Append(POW).Append(power); } } } else { // unary, don't add a '1' } } if (numeratorCount == 0) { numerator.Append(ONE_CHAR); } String result = null; if (denominatorCount == 0) { result = numerator.ToString(); } else { if (denominatorCount == 1) { result = numerator.Append(DIV).Append(denominator).ToString(); } else { result = numerator.Append(DIV).Append(LP).Append(denominator).Append(RP).ToString(); } } return(result); } // end unit of measure iteration
private void ExplodeRecursively(UnitOfMeasure unit, int level) { if (++Counter > MAX_RECURSIONS) { String msg = String.Format(MeasurementSystem.GetMessage("circular.references"), unit.Symbol); throw new Exception(msg); } // down a level level++; // scaling factor to abscissa unit double scalingFactor = unit.ScalingFactor; // explode the abscissa unit UnitOfMeasure abscissaUnit = unit.AbscissaUnit; UnitOfMeasure uom1 = abscissaUnit.UOM1; UnitOfMeasure uom2 = abscissaUnit.UOM2; int?exp1 = abscissaUnit.Exponent1; int?exp2 = abscissaUnit.Exponent2; // scaling if (PathExponents.Count > 0) { int lastExponent = PathExponents[PathExponents.Count - 1]; // compute the overall scaling factor double factor = 1; for (int i = 0; i < Math.Abs(lastExponent); i++) { factor = factor * scalingFactor; } if (lastExponent < 0) { MapScalingFactor = MapScalingFactor / factor; } else { MapScalingFactor = MapScalingFactor * factor; } } else { MapScalingFactor = scalingFactor; } if (uom1 == null) { if (!abscissaUnit.IsTerminal()) { // keep exploding down the conversion path double currentMapFactor = MapScalingFactor; MapScalingFactor = 1; ExplodeRecursively(abscissaUnit, STARTING_LEVEL); MapScalingFactor = MapScalingFactor * currentMapFactor; } else { // multiply out all of the exponents down the path int pathExponent = 1; foreach (int exp in PathExponents) { pathExponent = pathExponent * exp; } bool invert = pathExponent < 0 ? true : false; for (int i = 0; i < Math.Abs(pathExponent); i++) { AddTerm(abscissaUnit, invert); } } } else { // explode UOM #1 PathExponents.Add(exp1.Value); ExplodeRecursively(uom1, level); PathExponents.RemoveAt(level); } if (uom2 != null) { // explode UOM #2 PathExponents.Add(exp2.Value); ExplodeRecursively(uom2, level); PathExponents.RemoveAt(level); } // up a level level--; }
internal void Explode(UnitOfMeasure unit) { ExplodeRecursively(unit, STARTING_LEVEL); }
/// <summary>Create a quantity with an amount and unit of measure</summary> /// /// <param name="amount">Amount</param> /// <param name="uom">UnitOfMeasure</param> /// public Quantity(double amount, UnitOfMeasure uom) { Amount = amount; UOM = uom; }
/// <summary>Convert this quantity of a power unit using the specified base unit of /// measure.</summary> /// /// <param name="uom">Base UnitOfMeasure</param> /// /// <returns>Converted quantity</returns> /// public Quantity ConvertToPower(UnitOfMeasure uom) { UnitOfMeasure newUOM = UOM.ClonePower(uom); return(Convert(newUOM)); }
/// <summary>Convert this quantity with a product or quotient unit of measure to the /// specified units of measure.</summary> /// /// <param name="uom1">Multiplier or dividend UnitOfMeasure</param> /// <param name="uom2">Multiplicand or divisor UnitOfMeasure</param> /// /// <returns>Converted quantity</returns> /// public Quantity ConvertToPowerProduct(UnitOfMeasure uom1, UnitOfMeasure uom2) { UnitOfMeasure newUOM = UOM.ClonePowerProduct(uom1, uom2); return(Convert(newUOM)); }