Ejemplo n.º 1
0
        protected override void OnInit(EventArgs e)
        {
            var units = WebManager.GetUnits();

            foreach (WebUnitGroup webUnitGroup in _webManager.WebUnitGroups)
            {
                if (webUnitGroup.Enabled == false)
                {
                    continue;
                }

                // add a Unit Group (e.g. "Length", "Area") to the page

                UnitGroupControl unitGroupControl = (UnitGroupControl)Page.LoadControl("~/UnitGroupControl.ascx");
                unitGroupControl.Initialize(webUnitGroup);

                // add all enabled Units (e.g. "Meter", "Yard") to the Unit Group control

                foreach (WebUnit webUnit in _webManager.WebUnits)
                {
                    if (webUnit.Enabled)
                    {
                        var unit = units.First(p => p.ID == webUnit.UnitID);

                        if (unit.Type == webUnitGroup.UnitType)
                        {
                            UnitControl unitControl = (UnitControl)Page.LoadControl("~/UnitControl.ascx");
                            unitControl.Initialize(webUnit);
                            unitGroupControl.AddUnitControl(unitControl);
                        }
                    }
                }

                UnitGroupsPlaceHolder.Controls.Add(unitGroupControl);
            }
        }
Ejemplo n.º 2
0
        public List <FormattedUnit> RecalculateString(string unitName, string unitValue)
        {
            var result = new List <FormattedUnit>();

            // ensure that the unit name and unit value is okay

            if (string.IsNullOrEmpty(unitName) || string.IsNullOrEmpty(unitValue))
            {
                throw new Exception("The unit name or the unit value is empty!");
            }

            UnitID?unitID = unitName.GetUnitID();

            if (unitID.HasValue == false)
            {
                throw new Exception(string.Format("The unit name '{0}' is unknown!", unitName));
            }

            // find the source unit and all other units of the same type

            var sourceUnit = WebManager.GetUnit(unitID.Value);
            var unitType   = WebManager.GetUnit(unitID.Value).Type;

            // if user entered a number, recalculate all other units

            int?roundToDecimals = null;

            sourceUnit.Magnitude = double.NaN;

            var units = WebManager.GetUnits(unitType);
            var parts = WebManager.GetStringParts(unitValue);

            foreach (IUnit unit in units)
            {
                string unitvalue = string.Empty;

                foreach (var part in parts)
                {
                    string text     = part.Item1;
                    bool   isNumber = part.Item2;

                    if (isNumber)
                    {
                        sourceUnit.Magnitude = double.Parse(text);
                        roundToDecimals      = sourceUnit.GetDecimalPlaces() + 4;

                        var currentUnit = WebManager.Recalculate(sourceUnit).First(p => p.ID == unit.ID) as Unit;

                        if (roundToDecimals.HasValue)
                        {
                            var placesBeforeDecimalPoint = currentUnit.GetPlacesBeforeDecimalPoint();
                            if (placesBeforeDecimalPoint > 4)
                            {
                                roundToDecimals = placesBeforeDecimalPoint;
                            }

                            var scientific = currentUnit.ToScientificNotation(roundToDecimals.Value);
                            currentUnit.Magnitude = scientific.ToDouble();
                        }

                        unitvalue += ((FormattedUnit)currentUnit).UnitValue;
                    }
                    else
                    {
                        unitvalue += text;
                    }
                }

                FormattedUnit formattedUnit = (FormattedUnit)(Unit)unit;
                formattedUnit.UnitValue = unitvalue;

                result.Add(formattedUnit);
            }

            return(result);
        }