public void ConvertRounding()
    {
        ConvertExpression e = new("[in->ft]");

        Assert.IsNotNull(e);

        PreciseNumber feet = e.Evaluate(new PreciseNumber[] { new PreciseNumber(120m) });

        Assert.IsTrue(feet.HasValue);
        Assert.AreEqual(10, feet.Value);
    }
Exemple #2
0
    /// <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)),
        });