/// <summary> /// Gets the unit. /// </summary> /// <param name="current">The current.</param> /// <param name="levelFromCurrent">How much levels from the current unit? same factors are ignored</param> /// <returns></returns> public measureSystemUnitEntry GetUnit(measureSystemUnitEntry current, Int32 levelFromCurrent) { measureUnitLevelChange direction = measureUnitLevelChange.optimum; measureSystemUnitEntry candidate = current; measureSystemUnitEntry next = null; if (levelFromCurrent < 0) { direction = measureUnitLevelChange.goLower; } if (levelFromCurrent > 0) { direction = measureUnitLevelChange.goHigher; } if (direction == measureUnitLevelChange.optimum) { return(current); } levelFromCurrent = Math.Abs(levelFromCurrent); do { next = GetUnit(candidate, direction); levelFromCurrent--; if (candidate == next) { break; } candidate = next; } while (levelFromCurrent > 0); return(candidate); }
//public measureInteger():base() public override void convertToUnit(measureSystemUnitEntry targetUnit) { double fd = info.unit.GetFactorDistance(targetUnit); primValue = Convert.ToInt32(Convert.ToDouble(primValue) * fd); info.unit = targetUnit; }
public override void convertToUnit(measureSystemUnitEntry targetUnit) { double fd = targetUnit.GetFactorDistance(info.unit); //.GetFactorDistance(targetUnit); primValue = primValue * Convert.ToDecimal(fd); info.unit = targetUnit; }
/// <summary> /// Adds the unit. /// </summary> /// <param name="unit">The unit.</param> /// <param name="level">The level.</param> /// <param name="name">The name.</param> /// <param name="namePlural">The name plural.</param> /// <returns></returns> public measureSystemUnitEntry AddUnit(String sufix, Double factor = 0, String name = "milimeter", String namePlural = "*") { measureSystemUnitEntry output = new measureSystemUnitEntry(sufix, factor, name, this, namePlural); units.Add(output); //unitByFactor.Add(output.factor, output); return(output); }
//public virtual convertToUnit() /// <summary> /// Gets the optimal unit. /// </summary> /// <param name="current">The current.</param> /// <param name="length">The length.</param> /// <returns></returns> public measureSystemUnitEntry GetOptimalUnit(measureSystemUnitEntry current, Int32 length) { measureSystemUnitEntry candidate = current; measureSystemUnitEntry next = null; double factorDistance = 1; Int32 factorStrDistance = 0; Int32 estLength = length; Int32 difference = length - targetStringLength; if (length > targetStringLength) { while (estLength > targetStringLength) { next = GetUnit(candidate, measureUnitLevelChange.goHigher); if (candidate == next) { break; } factorStrDistance = next.GetFactorDistance(current).GetFactorStrLenChange(); estLength = length - factorStrDistance; difference = estLength - targetStringLength; if (difference < factorStrDistance) { break; } candidate = next; } } else if (length < targetStringLength) { while (estLength < targetStringLength) { next = GetUnit(candidate, measureUnitLevelChange.goLower); if (candidate == next) { break; } factorStrDistance = next.GetFactorDistance(current).GetFactorStrLenChange(); estLength = length + factorStrDistance; difference = targetStringLength - estLength; if (difference < factorStrDistance) { break; } candidate = next; } } return(candidate); }
/// <summary> /// Gets the factor distance. /// </summary> /// <param name="current">The current.</param> /// <param name="other">The other.</param> /// <returns></returns> public static double GetFactorDistance(this measureSystemUnitEntry current, measureSystemUnitEntry other) { double div = current.factor; if (div == 0.000000) { div = 1; } double distance = other.factor / div; if (double.IsInfinity(distance)) { return(other.factor); } return(distance); }
/// <summary> /// Gets next unit in specified direction <see cref="measureUnitLevelChange"/> /// </summary> /// <param name="current">The current.</param> /// <param name="direction">The direction to get next unit</param> /// <returns>Next defined unit level with different factor</returns> public measureSystemUnitEntry GetUnit(measureSystemUnitEntry current, measureUnitLevelChange direction) { var cindex = units.IndexOf(current); double cfactor = current.factor; measureSystemUnitEntry candidate = current; do { if (direction == measureUnitLevelChange.goLower) { if (cindex > 0) { cindex--; } else { break; } } else if (direction == measureUnitLevelChange.goHigher) { if (cindex < (units.Count - 1)) { cindex++; } else { break; } } else { break; } candidate = units[cindex]; if (candidate == null) { break; } } while (candidate.factor == cfactor); return(candidate); }
/// <summary> /// Gets the or make unit. /// </summary> /// <param name="tup">The tup.</param> /// <param name="unit">The unit.</param> /// <returns></returns> public measureSystemUnitEntry GetOrMakeUnit(Tuple <Enum, string, string, string, string> tup, measureSystemUnitEntry unit) { if (tup == null) { return(unit); } String __name = tup.Item2.toStringSafe(); if (imbSciStringExtensions.isNullOrEmptyString(__name)) { __name = tup.Item1.toStringSafe(); } if (units.ContainsKey(__name)) { return(units[tup.Item1.ToString()]); } else { measureSystemUnitEntry nr = null; if (unit == null) { nr = new measureSystemUnitEntry(tup.Item2, __name, imbSciStringExtensions.add(__name, "s")); nr.system = this; } else { nr = unit.Clone() as measureSystemUnitEntry; nr.nameSingular = tup.Item4; nr.unit = tup.Item3; } nr.system = this; nr.factor = 0; nr.setFormat(tup.Item4, tup.Item5); return(nr); } }
public override void convertToUnit(measureSystemUnitEntry targetUnit) { info.unit = targetUnit; }
public override void convertToUnit(measureSystemUnitEntry targetUnit) { //throw new NotImplementedException(); }
/// <summary> /// Gets the factor distance. /// </summary> /// <param name="current">The current.</param> /// <param name="direction">The direction.</param> /// <returns></returns> public double GetFactorDistance(measureSystemUnitEntry current, measureUnitLevelChange direction) { measureSystemUnitEntry next = GetUnit(current, direction); return(current.GetFactorDistance(next)); }