Beispiel #1
0
        public virtual T ConvertTo(TUnits unit)
        {
            var baseMagnitude = Magnitude * GetScaleFactor(Units) - UnitDataCache.GetOffset(Units);
            var scale         = GetScaleFactor(unit);
            var offset        = UnitDataCache.GetOffset(unit);

            return(_constructorFunc(baseMagnitude / scale + offset, unit));
        }
Beispiel #2
0
        public Velocity ConvertTo(string units)
        {
            var match = Regex.Match(units, @"^([a-zA-Z]+)\/([a-zA-Z]+)$");

            if (!match.Success)
            {
                throw new Exception($"Cannot convert to illegal format: {units}");
            }

            var length = match.Groups[1].Value;
            var time   = match.Groups[2].Value;

            return(ConvertTo(UnitDataCache.GetUnits <LengthUnits>(length), UnitDataCache.GetUnits <TimeUnits>(time)));
        }
Beispiel #3
0
        public static IUnit Parse(Type unitType, string measurement)
        {
            const string matchPattern = @"^([+-]?\s*(?:\d+\.?\d*|\d*\.\d+))(\D+)$";
            var          match        = Regex.Match(measurement.Trim(), matchPattern);

            if (!match.Success)
            {
                throw new InvalidUnitFormat(measurement);
            }

            var magnitude = double.Parse(match.Groups[1].Value.Replace(" ", ""));
            var symbol    = match.Groups[2].Value.Trim();

            switch (unitType.Name)
            {
            case nameof(Length):
                return(new Length(magnitude, UnitDataCache.GetUnits <LengthUnits>(symbol)));

            case nameof(Time):
                return(new Time(magnitude, UnitDataCache.GetUnits <TimeUnits>(symbol)));

            case nameof(Mass):
                return(new Mass(magnitude, UnitDataCache.GetUnits <MassUnits>(symbol)));

            case nameof(Luminosity):
                return(new Luminosity(magnitude, UnitDataCache.GetUnits <LuminosityUnits>(symbol)));

            case nameof(Temperature):
                return(new Temperature(magnitude, UnitDataCache.GetUnits <TemperatureUnits>(symbol)));

            case nameof(Information):
                return(new Information(magnitude, UnitDataCache.GetUnits <InformationUnits>(symbol)));

            case nameof(Current):
                return(new Current(magnitude, UnitDataCache.GetUnits <CurrentUnits>(symbol)));

            case nameof(AmountOfSubstance):
                return(new AmountOfSubstance(magnitude, UnitDataCache.GetUnits <AmountOfSubstanceUnits>(symbol)));

            case nameof(Velocity):
                var(lengthUnits, timeUnits) = Velocity.GetUnits(symbol);
                return(new Velocity(magnitude, lengthUnits, timeUnits));

            default:
                throw new UnknownUnitType(unitType);
            }
        }
Beispiel #4
0
 public virtual T ConvertTo(string unit) => ConvertTo(UnitDataCache.GetUnits <TUnits>(unit));
Beispiel #5
0
 public virtual double GetScaleFactor(TUnits unit) => UnitDataCache.GetScaleFactor(unit);
Beispiel #6
0
 public static string GetSymbol <T>(T unitType) where T : Enum
 {
     return(UnitDataCache.GetSymbol(unitType));
 }