public FormattedUnit FindUnitFromUrl(string url)
        {
            // from "https://www.unitcandy.com?12.345m"
            // find 12.345 meters

            FormattedUnit result = null;

            int posQuestionMark = url.IndexOf('?');
            int posAmpersand    = url.IndexOf('&');

            string inputstring = string.Empty;

            if (posQuestionMark >= 0)
            {
                if (posAmpersand < 0)
                {
                    inputstring = url.Substring(posQuestionMark).TrimStart('?');
                }
                else if (posAmpersand > posQuestionMark)
                {
                    inputstring = url.Substring(posQuestionMark, posAmpersand - posQuestionMark).TrimStart('?');
                }
            }

            if (!string.IsNullOrWhiteSpace(inputstring))
            {
                result = FindUnit(inputstring, null);
            }

            return(result);
        }
        public FormattedUnit FindUnit(string inputstring, string unitName)
        {
            FormattedUnit result = null;

            if (!string.IsNullOrWhiteSpace(unitName))
            {
                string remainder;
                var    number = UnitConverterUtility.ParseDouble(inputstring, out remainder);

                if (number.HasValue)
                {
                    if (string.IsNullOrWhiteSpace(remainder))
                    {
                        inputstring = inputstring + unitName;
                    }
                    else
                    {
                        inputstring = inputstring.Replace(remainder, unitName);
                    }
                }
            }

            IEnumerable <IUnit> units;

            UnitConverterUtility.TryParseUnits(inputstring, out units);

            if (units != null)
            {
                if (units.Any())
                {
                    result = (FormattedUnit)(Unit)units.First();
                }
            }

            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);
        }