/// <summary> /// Converts an ADAPT NumericRepresentation value with an included UnitOfMeasure to the requested units /// </summary> /// <param name="value"></param> /// <param name="mapper"></param> /// <returns></returns> public static double?AsConvertedDouble(this NumericRepresentationValue value, string targetUnitCode) { if (value == null) { return(null); } else if (value.Value.UnitOfMeasure == null) { return(value.Value.Value); //Return the unconverted value } else { RepresentationUnitSystem.UnitOfMeasure sourceUOM = RepresentationUnitSystem.InternalUnitSystemManager.Instance.UnitOfMeasures[value.Value.UnitOfMeasure.Code]; RepresentationUnitSystem.UnitOfMeasure targetUOM = RepresentationUnitSystem.InternalUnitSystemManager.Instance.UnitOfMeasures[targetUnitCode]; if (sourceUOM == null || targetUOM == null) { return(value.Value.Value); //Return the unconverted value } else { RepresentationUnitSystem.UnitOfMeasureConverter converter = new RepresentationUnitSystem.UnitOfMeasureConverter(); return(converter.Convert(sourceUOM, targetUOM, value.Value.Value)); } } }
public static double ConvertValue(this double n, string srcUnitCode, string dstUnitCode) { RepresentationUnitSystem.UnitOfMeasure sourceUOM = RepresentationUnitSystem.InternalUnitSystemManager.Instance.UnitOfMeasures[srcUnitCode]; RepresentationUnitSystem.UnitOfMeasure targetUOM = RepresentationUnitSystem.InternalUnitSystemManager.Instance.UnitOfMeasures[dstUnitCode]; if (sourceUOM == null || targetUOM == null) { return(n); //Return the unconverted value } else { //The plugin uses "count" instead of "seeds". Alter the codes so that the conversion will succeed if there are mismatches if (srcUnitCode.StartsWith("seeds")) { srcUnitCode = srcUnitCode.Replace("seeds", "count"); sourceUOM = RepresentationUnitSystem.InternalUnitSystemManager.Instance.UnitOfMeasures[srcUnitCode]; } if (dstUnitCode.StartsWith("seeds")) { dstUnitCode = dstUnitCode.Replace("seeds", "count"); targetUOM = RepresentationUnitSystem.InternalUnitSystemManager.Instance.UnitOfMeasures[dstUnitCode]; } RepresentationUnitSystem.UnitOfMeasureConverter converter = new RepresentationUnitSystem.UnitOfMeasureConverter(); return(converter.Convert(sourceUOM, targetUOM, n)); } }