Ejemplo n.º 1
0
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            Unit   unit;
            double doubleValue;

            if (value is PhysicalValue)
            {
                PhysicalValue physicalValue = (PhysicalValue)value;
                unit        = physicalValue.Unit;
                doubleValue = physicalValue.Value;
            }
            else if (value is Quantity)
            {
                Quantity quantity = (Quantity)value;
                unit        = quantity.Unit;
                doubleValue = quantity.Value;
            }
            else if (value is double)
            {
                unit        = DefaultUnit;
                doubleValue = (double)value;
            }
            else
            {
                return(null);
            }

            return(PhysicalValue.Format(doubleValue, unit, Format, MinimumPrefix, MaximumPrefix, DefaultPrefix, NumberOfDigits, Precision, MaximumMagnitude));
        }
Ejemplo n.º 2
0
        /// <summary>
        ///     Formats the value with the format options of the control.
        /// </summary>
        /// <param name="value">The value.</param>
        protected internal string FormatValue(double value)
        {
            var displayedUnit = Unit;
            var precision     = Precision;
            var maxMagnitude  = MaximumMagnitude;

            if (DisplayUnit.HasValue && DisplayUnit.Value != Unit)
            {
                value         = Conversions.Convert(value, Unit, DisplayUnit.Value);
                precision     = Conversions.Convert(Precision, Unit, DisplayUnit.Value);
                maxMagnitude  = Conversions.Convert(MaximumMagnitude, Unit, DisplayUnit.Value);
                displayedUnit = DisplayUnit.Value;
            }
            var digits = NumberOfFractionDigits;

            if (PhysicalValueFormat == PhysicalValueFormat.SIUsingSignificantDigits)
            {
                digits = NumberOfSignificantDigits;
            }

            return(PhysicalValue.Format(value, displayedUnit, PhysicalValueFormat, MinimumSIPrefix, MaximumSIPrefix, ParsePrefix, digits, precision, maxMagnitude));
        }