コード例 #1
0
        /// <summary>
        ///     Converts <see cref="UnitsNet.Pressure.Value" /> at <see cref="Reference" /> to <see cref="double" /> at
        ///     <see cref="BaseReference" />
        /// </summary>
        /// <returns>The value of pressure at the <see cref="BaseReference" /></returns>
        private double AsBaseReference()
        {
            switch (Reference)
            {
            case PressureReference.Absolute:
            {
                if (Pressure.Value < 0)
                {
                    throw new ArgumentOutOfRangeException("Absolute pressure cannot be less than zero.");
                }

                return(Pressure.Value);
            }

            case PressureReference.Gauge:
            {
                if (Pressure.Value * -1 > AtmosphericPressure.ToUnit(Pressure.Unit).Value)
                {
                    throw new ArgumentOutOfRangeException("Absolute pressure cannot be less than zero.");
                }

                return(AtmosphericPressure.ToUnit(Pressure.Unit).Value + Pressure.Value);
            }

            case PressureReference.Vacuum:
            {
                if (Pressure.Value > AtmosphericPressure.ToUnit(Pressure.Unit).Value)
                {
                    throw new ArgumentOutOfRangeException("Absolute pressure cannot be less than zero.");
                }

                return(AtmosphericPressure.ToUnit(Pressure.Unit).Value - Pressure.Value);
            }

            default:
                throw new NotImplementedException($"Can not convert {Reference} to base reference.");
            }
        }
コード例 #2
0
        /// <summary>
        ///     Converts <see cref="UnitsNet.Pressure.Value" /> to <see cref="double" /> at <see cref="PressureReference" />
        /// </summary>
        /// <param name="reference">The <see cref="PressureReference" /> to convert <see cref="ReferencePressure" /> to.</param>
        /// <returns>The value of pressure at <see cref="PressureReference" /></returns>
        private double AsBaseNumericType(PressureReference reference)
        {
            var baseReferenceValue = AsBaseReference();

            if (Reference == reference)
            {
                return(Pressure.Value);
            }

            var negatingValue = Reference == PressureReference.Vacuum ? -1 : 1;

            switch (reference)
            {
            case PressureReference.Absolute: return(baseReferenceValue);

            case PressureReference.Gauge: return(baseReferenceValue - AtmosphericPressure.ToUnit(Pressure.Unit).Value);

            case PressureReference.Vacuum: return(AtmosphericPressure.ToUnit(Pressure.Unit).Value - negatingValue * baseReferenceValue);

            default:
                throw new NotImplementedException($"Can not convert {Reference} to {reference}.");
            }
        }