public List <FormattedUnit> Recalculate(string unitName, string unitValue) { var result = new List <FormattedUnit>(); // ensure that the unit name and unit value is okay if (string.IsNullOrEmpty(unitName) || string.IsNullOrEmpty(unitValue)) { throw new Exception("The unit name or the unit value is empty!"); } UnitID?unitID = unitName.GetUnitID(); if (unitID.HasValue == false) { throw new Exception(string.Format("The unit name '{0}' is unknown!", unitName)); } // find the source unit and all other units of the same type var sourceUnit = WebManager.GetUnit(unitID.Value); // if user entered a number, recalculate all other units int?roundToDecimals = null; sourceUnit.Magnitude = double.NaN; IEnumerable <IUnit> units = new List <IUnit>() { sourceUnit }; if (_webman.IsNumber(unitValue)) { sourceUnit.Magnitude = double.Parse(unitValue); roundToDecimals = sourceUnit.GetDecimalPlaces() + 4; if (sourceUnit.Magnitude >= sourceUnit.MinValue && sourceUnit.Magnitude <= sourceUnit.MaxValue) { units = WebManager.Recalculate(sourceUnit); } } // create list of results to return to the caller foreach (IUnit unit in units) { if (unit.Type != sourceUnit.Type) { continue; } var webUnit = _webman.GetWebUnit(unit.ID); if (webUnit == null) { continue; } if (webUnit.Enabled == false) { continue; } if (roundToDecimals.HasValue) { var placesBeforeDecimalPoint = unit.GetPlacesBeforeDecimalPoint(); if (placesBeforeDecimalPoint > 4) { roundToDecimals = placesBeforeDecimalPoint; } var scientific = unit.ToScientificNotation(roundToDecimals.Value); unit.Magnitude = scientific.ToDouble(); } result.Add((FormattedUnit)(Unit)unit); } return(result); }
public List <FormattedUnit> RecalculateString(string unitName, string unitValue) { var result = new List <FormattedUnit>(); // ensure that the unit name and unit value is okay if (string.IsNullOrEmpty(unitName) || string.IsNullOrEmpty(unitValue)) { throw new Exception("The unit name or the unit value is empty!"); } UnitID?unitID = unitName.GetUnitID(); if (unitID.HasValue == false) { throw new Exception(string.Format("The unit name '{0}' is unknown!", unitName)); } // find the source unit and all other units of the same type var sourceUnit = WebManager.GetUnit(unitID.Value); var unitType = WebManager.GetUnit(unitID.Value).Type; // if user entered a number, recalculate all other units int?roundToDecimals = null; sourceUnit.Magnitude = double.NaN; var units = WebManager.GetUnits(unitType); var parts = WebManager.GetStringParts(unitValue); foreach (IUnit unit in units) { string unitvalue = string.Empty; foreach (var part in parts) { string text = part.Item1; bool isNumber = part.Item2; if (isNumber) { sourceUnit.Magnitude = double.Parse(text); roundToDecimals = sourceUnit.GetDecimalPlaces() + 4; var currentUnit = WebManager.Recalculate(sourceUnit).First(p => p.ID == unit.ID) as Unit; if (roundToDecimals.HasValue) { var placesBeforeDecimalPoint = currentUnit.GetPlacesBeforeDecimalPoint(); if (placesBeforeDecimalPoint > 4) { roundToDecimals = placesBeforeDecimalPoint; } var scientific = currentUnit.ToScientificNotation(roundToDecimals.Value); currentUnit.Magnitude = scientific.ToDouble(); } unitvalue += ((FormattedUnit)currentUnit).UnitValue; } else { unitvalue += text; } } FormattedUnit formattedUnit = (FormattedUnit)(Unit)unit; formattedUnit.UnitValue = unitvalue; result.Add(formattedUnit); } return(result); }