public List <FormattedUnit> RecalculateWithHelperAction(string unitName, string unitValue, string action)
        {
            if (!_webman.IsNumber(unitValue))
            {
                unitValue = "0";
            }

            try {
                switch (action)
                {
                case "+1":
                    unitValue = (double.Parse(unitValue) + 1).ToString();
                    break;

                case "-1":
                    unitValue = (double.Parse(unitValue) - 1).ToString();
                    break;

                case "x10":
                    unitValue = (double.Parse(unitValue) * 10).ToString();
                    break;

                case "/10":
                    unitValue = (double.Parse(unitValue) / 10).ToString();
                    break;

                case "x2":
                    unitValue = (double.Parse(unitValue) * 2).ToString();
                    break;

                case "/2":
                    unitValue = (double.Parse(unitValue) / 2).ToString();
                    break;

                case "Clear":
                    unitValue = "0";
                    break;

                case "Min":
                    unitValue = WebManager.GetUnit(unitName.GetUnitID().Value).MinValue.ToString();
                    break;

                case "Max":
                    unitValue = WebManager.GetUnit(unitName.GetUnitID().Value).MaxValue.ToString();
                    break;

                default:
                    break;
                }
            } catch (Exception) {
                unitValue = "0";
            }

            return(Recalculate(unitName, unitValue));
        }
        public String GetCopyToClipboardText(string unitName, string unitValue)
        {
            StringBuilder result = new StringBuilder();

            IUnit        sourceUnit   = null;
            WebUnitGroup webUnitGroup = null;

            UnitID?unitID = unitName.GetUnitID();

            if (unitID.HasValue)
            {
                sourceUnit   = WebManager.GetUnit(unitID.Value);
                webUnitGroup = _webman.WebUnitGroups.FirstOrDefault(p => p.UnitType == sourceUnit.Type);
            }

            var formattedUnits = new List <FormattedUnit>();

            if (webUnitGroup != null)
            {
                result.AppendLine(webUnitGroup.GroupName);
                result.AppendLine(webUnitGroup.Description);
                result.AppendLine();

                formattedUnits = Recalculate(unitName, unitValue);

                foreach (FormattedUnit formattedUnit in formattedUnits)
                {
                    if (formattedUnit.UnitType.GetUnitType() == sourceUnit.Type)
                    {
                        result.AppendFormat("{0}:\t{1}{2}", formattedUnit.FriendlyUnitName, formattedUnit.UnitValue, formattedUnit.UnitSymbol);
                        result.AppendLine();
                    }
                }

                result.AppendLine();
                result.Append("https://www.unitcandy.com?" + unitValue + unitName);
                result.AppendLine();
            }
            else
            {
                result.AppendLine("UnitCandy");
                result.AppendLine();
                result.AppendLine("UnitCandy is trying to help you with your everyday unit conversion needs. It contains many units of measurement commonly encountered and converts them to other units fast and accurate. UnitCandy was designed to be nice-looking and user friendly.");
                result.AppendLine();
            }

            return(result.ToString());
        }
Пример #3
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);
            }
        }
        public List <FormattedUnit> Recalculate(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);

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

            int?roundToDecimals = null;

            sourceUnit.Magnitude = double.NaN;

            IEnumerable <IUnit> units = new List <IUnit>()
            {
                sourceUnit
            };

            if (_webman.IsNumber(unitValue))
            {
                sourceUnit.Magnitude = double.Parse(unitValue);
                roundToDecimals      = sourceUnit.GetDecimalPlaces() + 4;

                if (sourceUnit.Magnitude >= sourceUnit.MinValue && sourceUnit.Magnitude <= sourceUnit.MaxValue)
                {
                    units = WebManager.Recalculate(sourceUnit);
                }
            }

            // create list of results to return to the caller

            foreach (IUnit unit in units)
            {
                if (unit.Type != sourceUnit.Type)
                {
                    continue;
                }

                var webUnit = _webman.GetWebUnit(unit.ID);
                if (webUnit == null)
                {
                    continue;
                }
                if (webUnit.Enabled == false)
                {
                    continue;
                }

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

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

                result.Add((FormattedUnit)(Unit)unit);
            }

            return(result);
        }
        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);
        }