Пример #1
0
        // Throw if a given unit does exist
        private void ValidateNewSynonym(UnitFactorSynonyms synonyms)
        {
            var preExistingUnit = Units.FindUnit(synonyms);

            if (preExistingUnit != null)
            {
                throw new UnitAlreadyExistsException(preExistingUnit.ToString());
            }
        }
 // Find if some synonym of a given UnitFactor is included in this UnitFactor
 internal bool Contains(UnitFactorSynonyms synonyms)
 {
     foreach (var syn in synonyms.Synonyms)
     {
         if (this.Contains(syn))
         {
             return(true);
         }
     }
     return(false);
 }
Пример #3
0
        //Reference: https://www.thoughtco.com/temperature-conversion-formulas-609324
        public static double AToB(IBaseUnitConverter unitConverter, double value, string startUnit, string endUnit)
        {
            UnitFactorSynonyms startUnitSyn = unitConverter.SupportedUnits.SingleOrDefault(r => r.Contains(startUnit));
            UnitFactorSynonyms endUnitSyn   = unitConverter.SupportedUnits.SingleOrDefault(r => r.Contains(endUnit));

            if (startUnitSyn is default(UnitFactorSynonyms) || endUnitSyn is default(UnitFactorSynonyms))
            {
                return(value);
            }

            if (startUnitSyn.Contains(CELSIUS_KEY) && endUnitSyn.Contains(FAHRENHEIT_KEY))
            {
                //Celsius to Fahrenheit : °F = 9/5 (°C) + 32
                return(((9d / 5) * value) + 32);
            }
            else if (startUnitSyn.Contains(KELVIN_KEY) && endUnitSyn.Contains(FAHRENHEIT_KEY))
            {
                //Kelvin to Fahrenheit: °F = 9 / 5(K - 273.15) + 32
                return((9d / 5) * (value - 273.15) + 32);
            }
            else if (startUnitSyn.Contains(FAHRENHEIT_KEY) && endUnitSyn.Contains(CELSIUS_KEY))
            {
                //Fahrenheit to Celsius: °C = 5/9 (°F - 32)
                return((5d / 9) * (value - 32));
            }
            else if (startUnitSyn.Contains(CELSIUS_KEY) && endUnitSyn.Contains(KELVIN_KEY))
            {
                //Celsius to Kelvin: K = °C + 273.15
                return(value + 273.15);
            }
            else if (startUnitSyn.Contains(KELVIN_KEY) && endUnitSyn.Contains(CELSIUS_KEY))
            {
                //Kelvin to Celsius: °C = K - 273.15
                return(value - 273.15);
            }
            else if (startUnitSyn.Contains(FAHRENHEIT_KEY) && endUnitSyn.Contains(KELVIN_KEY))
            {
                //Fahrenheit to Kelvin: K = 5 / 9(°F - 32) + 273.15
                return((5d / 9) * (value - 32) + 273.15);
            }
            //Used "else" for corner case "both units are same"
            else
            {
                return(value);
            }
        }
Пример #4
0
 /// <summary>
 /// Add a new unit with a new set of synonyms
 /// </summary>
 /// <param name="synonyms">Object of synonyms</param>
 /// <param name="factor"></param>
 public void AddUnit(UnitFactorSynonyms synonyms, double factor)
 {
     ValidateNewSynonym(synonyms);
     Units.Add(synonyms, factor);
 }