コード例 #1
0
        /// <summary>
        /// Performs a unit conversion from the standard value into the specified unit.
        /// </summary>
        /// <param name="val">The value to convert.</param>
        /// <param name="unitto">The name of the unit that the value is to be converted to.</param>
        /// <param name="output">The converted value.</param>
        /// <returns>Unit result value.</returns>
        public UnitResult ConvertFromStandard(double val, string unitto, out double output)
        {
            double x = val;

            // Default to the fail safe value.
            output = FAILSAFE_VALUE;

            IUnitEntry unit_to = GetUnitBySymbol(unitto);

            // Make sure both units are real units.
            if (unit_to == null)
            {
                return(UnitResult.BadUnit);
            }

            try
            {
                // Convert to the new unit from the standard
                x = x - unit_to.PreAdder;
                if (unit_to.Multiplier > 0.0)
                {
                    x = x * Math.Pow(unit_to.Multiplier, -1);
                }
                x = x - unit_to.Adder;

                output = x;
            }
            catch
            {
                // Probably overflowed or something.
                return(UnitResult.BadValue);
            }

            return(UnitResult.NoError);
        }
コード例 #2
0
        /// <summary>
        /// Given a value and the current unit, converts the value back to the standard.
        /// </summary>
        /// <param name="val">Value to convert.</param>
        /// <param name="unitfrom">Name of the current units the value is in.</param>
        /// <param name="output">Variable to hold the converted value.</param>
        /// <returns>A unit result value.</returns>
        public UnitResult ConvertToStandard(double val, string unitfrom, out double output)
        {
            double x = val;

            // Default to the fail safe value.
            output = FAILSAFE_VALUE;

            IUnitEntry unit_from = GetUnitBySymbol(unitfrom);

            // Make sure both units are real units.
            if (unit_from == null)
            {
                return(UnitResult.BadUnit);
            }

            try
            {
                // Convert the value back to the standard
                x = x + unit_from.PreAdder;
                if (unit_from.Multiplier > 0.0)
                {
                    x = x * unit_from.Multiplier;
                }
                x = x + unit_from.Adder;

                output = x;
            }
            catch
            {
                // Probably overflowed or something.
                return(UnitResult.BadValue);
            }
            return(UnitResult.NoError);
        }
コード例 #3
0
        /// <summary>
        /// Gets a value that determines whether the given units are compatible or not.
        /// </summary>
        /// <param name="unitSymbol1">Symbol for the first unit.</param>
        /// <param name="unitSymbol2">Symbol for the second unit.</param>
        /// <returns>True if units are compatible, else false.</returns>
        public bool CompatibleUnits(string unitSymbol1, string unitSymbol2)
        {
            IUnitEntry u1 = GetUnitBySymbol(unitSymbol1);
            IUnitEntry u2 = GetUnitBySymbol(unitSymbol2);

            if (u1 == null || u2 == null)
            {
                return(false);
            }

            return(GetUnitGroup(u1.Name) == GetUnitGroup(u2.Name));
        }
コード例 #4
0
		internal DataString(UnitConverter uc, string unitSymbol)
		{
			// Reference the unit converter that created us.
			m_uc = uc;

			m_flags = DataStringFlags.None;

			// Default unit is the blank unit
			m_unit = m_uc.GetUnitBySymbol(unitSymbol);

			if (m_unit == null)
			{
				m_unit = m_uc.GetUnitBySymbol("");
			}

			m_value = 0.0;
		}
コード例 #5
0
        private void txtInput_TextChanged(object sender, System.EventArgs e)
        {
            double val;
            double outval;
            string in_unit;

            UnitResult res = UnitResult.NoError;

            res = uc.ParseUnitString(this.txtInput.Text, out val, out in_unit);

            if (res == UnitResult.BadUnit)
            {
                txtOutput.Text = "Bad input unit.";
                return;
            }
            else
            {
                if (res == UnitResult.BadValue)
                {
                    txtOutput.Text = "Bad input value.";
                    return;
                }
            }

            IUnitEntry out_unit = uc.GetUnitBySymbol(txtUnitTo.Text);

            if (out_unit == null)
            {
                txtOutput.Text = "Bad output unit.";
                return;
            }

            if (!uc.CompatibleUnits(in_unit, txtUnitTo.Text))
            {
                txtOutput.Text = "Units are of different types.";
                return;
            }

            res = uc.ConvertUnits(val, in_unit, txtUnitTo.Text, out outval);

            this.txtOutput.Text = outval.ToString( ) + " " + out_unit.DefaultSymbol;
        }
コード例 #6
0
		/// <summary>
		/// Gets the current value of the data string as string form in the specified units.
		/// </summary>
		/// <param name="unitSymbol">Unit to return the data string as.</param>
		/// <param name="output">Varialbe to hold the output of the method call.</param>
		/// <returns>Unit result code.</returns>
		public UnitResult GetValueAs(string unitSymbol, out string output)
		{
			double d = 0.0;

			output = "";

			//Convert the standard stored value into the current unit.
			UnitResult res = m_uc.ConvertFromStandard(m_value, unitSymbol, out d);
			if (res != UnitResult.NoError)
			{
				return res;
			}

			//Get a reference to the unit.
			IUnitEntry unit = m_uc.GetUnitBySymbol(unitSymbol);

			//Output the result
			output = d.ToString() + " " + unit.DefaultSymbol;
			return res;
		}
コード例 #7
0
        /// <summary>
        /// Performs a unit conversion between two units, given a value to convert.
        /// </summary>
        /// <param name="val">The value to convert.</param>
        /// <param name="unitfrom">The name of the unit the value is currently in.</param>
        /// <param name="unitto">The name of the unit that the value is to be converted to.</param>
        /// <param name="output">The converted value.</param>
        /// <returns>Unit result value.</returns>
        public UnitResult ConvertUnits(double val, string unitfrom, string unitto, out double output)
        {
            double x = val;

            // Default to the fail safe value.
            output = FAILSAFE_VALUE;

            IUnitEntry unit_from = GetUnitBySymbol(unitfrom);
            IUnitEntry unit_to   = GetUnitBySymbol(unitto);

            // Make sure both units are real units.
            if ((unit_from == null) || (unit_to == null))
            {
                return(UnitResult.BadUnit);
            }

            // Make sure the units are of the same group
            if (!this.CompatibleUnits(unit_from.Name, unit_to.Name))
            {
                return(UnitResult.UnitMismatch);
            }

            UnitResult conv_res;

            conv_res = ConvertToStandard(x, unit_from.Name, out x);
            if (conv_res != UnitResult.NoError)
            {
                return(conv_res);
            }

            conv_res = ConvertFromStandard(x, unit_to.Name, out x);
            if (conv_res != UnitResult.NoError)
            {
                return(conv_res);
            }

            output = x;

            return(UnitResult.NoError);
        }
コード例 #8
0
		/// <summary>
		/// Sets the unit of the data string.
		/// </summary>
		/// <param name="unitSymbol">Symbol of unit to set the datastring to.</param>
		/// <returns>Unit result value.</returns>
		public UnitResult SetUnit(string unitSymbol)
		{
			IUnitEntry unit = m_uc.GetUnitBySymbol(unitSymbol);

			if (unit == null)
			{
				return UnitResult.BadUnit;
			}
			else
			{
				//If its the same don't touch it.
				if (unit == m_unit)
				{
					return UnitResult.NoError;
				}

				m_unit = unit;

				OnUnitChanged?.Invoke(this, EventArgs.Empty);

				return UnitResult.NoError;
			}
		}