public void ConvertFromMilesPerHourToMetersPerSecTest() { var converter = new SpeedConverter(SpeedUnit.MilesPerHour, SpeedUnit.MetersPerSec); Assert.AreEqual(0, converter.Convert(0)); Assert.AreEqual(0.44704, converter.Convert(1)); Assert.AreEqual(0.89408, converter.Convert(2)); }
public void ConvertFromMetersPerSecToMilesPerHourTest() { var converter = new SpeedConverter(SpeedUnit.MetersPerSec, SpeedUnit.MilesPerHour); Assert.AreEqual(0, converter.Convert(0)); Assert.AreEqual(2.23693629, converter.Convert(1)); Assert.AreEqual(4.47387258, converter.Convert(2)); }
public object Convert(ConverterType type, object value) { switch (type) { case ConverterType.Temperature: _logger.Debug("Temperature converter started"); return(TemperatureConverter.Convert(value)); case ConverterType.Speed: _logger.Debug("Speed converter started"); return(SpeedConverter.Convert(value)); case ConverterType.DegreeToDirection: _logger.Debug("Degree to direction converter started"); return(DegreeToDirectionConverter.Convert(value)); case ConverterType.Pressure: _logger.Debug("Pressure converter started"); return(PressureConverter.Convert(value)); default: _logger.Error("Unknown converter type"); return(null); } }
public void SpeedConverter_Convert() { decimal result = SpeedConverter.Convert(SpeedUnit.MeterPerSecond, SpeedUnit.KilometerPerHour, 60); Assert.AreEqual(216m, result); result = SpeedConverter.Convert(SpeedUnit.MilePerHour, SpeedUnit.KilometerPerHour, 60); // 96.560639999999992d Assert.AreEqual(96.56064m, result); }
public void Example() { double result = SpeedConverter.Convert( SpeedUnit.MeterPerSecond, SpeedUnit.KilometerPerHour, 60); Assert.AreEqual(216d, result); result = SpeedConverter.Convert( SpeedUnit.MilePerHour, SpeedUnit.KilometerPerHour, 60); Assert.AreEqual(96.560639999999992d, result); }
public void WithUnitAndUnitInput(Type targetType, UnitInput inputOptions, object expected) { var converter = new SpeedConverter { Unit = SpeedUnit.MetresPerSecond, UnitInput = inputOptions }; var length = Speed.FromMetresPerSecond(1.2); var actual = converter.Convert(length, targetType, null, CultureInfo.InvariantCulture); Assert.AreEqual(expected, actual); }
public void WithUnitAndSymbolFormat(Type targetType, SymbolFormat format, object expected) { var converter = new SpeedConverter { Unit = SpeedUnit.MetresPerSecond, UnitInput = UnitInput.SymbolRequired, SymbolFormat = format }; var length = Speed.FromMetresPerSecond(1.2); var actual = converter.Convert(length, targetType, null, CultureInfo.InvariantCulture); Assert.AreEqual(expected, actual); }
public void ValueFormatUnitAndUnitFormat() { var converter = new SpeedConverter { ValueFormat = "F2", Unit = SpeedUnit.MillimetresPerSecond, SymbolFormat = SymbolFormat.SignedSuperScript }; var convert = converter.Convert(Speed.FromMetresPerMinute(12.34), typeof(string), null, CultureInfo.InvariantCulture); Assert.AreEqual("205.67\u00A0mm⋅s⁻¹", convert); Assert.AreEqual(SpeedUnit.MillimetresPerSecond, converter.Unit); Assert.AreEqual(UnitInput.SymbolRequired, converter.UnitInput); }
/// <summary>Convert the numbers to the new unit.</summary> /// <param name="numbers">The numbers used in the convertion.</param> /// <returns>The result of the convertion execution.</returns> /// <exception cref="ArgumentNullException">When numbers is null.</exception> /// <exception cref="ArgumentException">When the length of numbers do not equal <see cref="ArgumentCount"/>.</exception> public double Convert(double[] numbers) { base.Validate(numbers); double fromValue = numbers[0]; switch (current.UnitType) { case UnitType.Length: return(LengthConverter.Convert( (LengthUnit)current.FromUnit, (LengthUnit)current.ToUnit, fromValue)); case UnitType.Mass: return(MassConverter.Convert( (MassUnit)current.FromUnit, (MassUnit)current.ToUnit, fromValue)); case UnitType.Speed: return(SpeedConverter.Convert( (SpeedUnit)current.FromUnit, (SpeedUnit)current.ToUnit, fromValue)); case UnitType.Temperature: return(TemperatureConverter.Convert( (TemperatureUnit)current.FromUnit, (TemperatureUnit)current.ToUnit, fromValue)); case UnitType.Time: return(TimeConverter.Convert( (TimeUnit)current.FromUnit, (TimeUnit)current.ToUnit, fromValue)); case UnitType.Volume: return(VolumeConverter.Convert( (VolumeUnit)current.FromUnit, (VolumeUnit)current.ToUnit, fromValue)); default: throw new ArgumentOutOfRangeException("numbers"); } }
/// <summary>Convert the numbers to the new unit.</summary> /// <param name="numbers">The numbers used in the conversion.</param> /// <returns>The result of the conversion execution.</returns> /// <exception cref="ArgumentNullException">When numbers is null.</exception> /// <exception cref="ArgumentException">When the length of numbers do not equal <see cref="ArgumentCount"/>.</exception> public PreciseNumber Evaluate(PreciseNumber[] operands) { ((IExpression)this).Validate(operands); PreciseNumber fromValue = operands[0]; if (!fromValue.HasValue) { return(fromValue); } return(_current.UnitType switch { UnitType.Length => new PreciseNumber(LengthConverter.Convert((LengthUnit)_current.FromUnit, (LengthUnit)_current.ToUnit, fromValue.Value)), UnitType.Mass => new PreciseNumber(MassConverter.Convert((MassUnit)_current.FromUnit, (MassUnit)_current.ToUnit, fromValue.Value)), UnitType.Speed => new PreciseNumber(SpeedConverter.Convert((SpeedUnit)_current.FromUnit, (SpeedUnit)_current.ToUnit, fromValue.Value)), UnitType.Temperature => new PreciseNumber(TemperatureConverter.Convert((TemperatureUnit)_current.FromUnit, (TemperatureUnit)_current.ToUnit, fromValue.Value)), UnitType.Time => new PreciseNumber(TimeConverter.Convert((TimeUnit)_current.FromUnit, (TimeUnit)_current.ToUnit, fromValue.Value)), UnitType.Volume => new PreciseNumber(VolumeConverter.Convert((VolumeUnit)_current.FromUnit, (VolumeUnit)_current.ToUnit, fromValue.Value)), _ => throw new ArgumentOutOfRangeException(nameof(operands)), });
private void Convert(double value) { switch (SelectedOp) { case "Length": { LengthunitConverter unit = new LengthunitConverter(); Result = unit.Convert(value, SelectedFrom, SelectedTo).ToString(); break; } case "Mass and Weight": { MassConverter unit = new MassConverter(); Result = unit.Convert(value, SelectedFrom, SelectedTo).ToString(); break; } case "Power": { PowerConverter unit = new PowerConverter(); Result = unit.Convert(value, SelectedFrom, SelectedTo).ToString(); break; } case "Pressure": { PressureConverter unit = new PressureConverter(); Result = unit.Convert(value, SelectedFrom, SelectedTo).ToString(); break; } case "Energy": { EnergyConveter unit = new EnergyConveter(); Result = unit.Convert(value, SelectedFrom, SelectedTo).ToString(); break; } case "Temperature": { TemperatureConverter unit = new TemperatureConverter(); Result = unit.Convert(value, SelectedFrom, SelectedTo).ToString(); break; } case "Volume": { VolumeConverter unit = new VolumeConverter(); Result = unit.Convert(value, SelectedFrom, SelectedTo).ToString(); break; } case "Angle": { AngleConverter unit = new AngleConverter(); Result = unit.Convert(value, SelectedFrom, SelectedTo).ToString(); break; } case "Area": { AreaConverter unit = new AreaConverter(); Result = unit.Convert(value, SelectedFrom, SelectedTo).ToString(); break; } case "Speed": { SpeedConverter unit = new SpeedConverter(); Result = unit.Convert(value, SelectedFrom, SelectedTo).ToString(); break; } case "Time": { TimeunitsConverter unit = new TimeunitsConverter(); Result = unit.Convert(value, SelectedFrom, SelectedTo).ToString(); break; } } }