Пример #1
0
        public override string ToString()
        {
            if (Type != UnitType.None)
            {
                return(Value + UnitTypeHelper.GetSymbol(Type));
            }

            return(Value.ToString());
        }
Пример #2
0
        // 50%
        // 50m
        // 50 m

        public static Unit Parse(string text)
        {
            if (text is null)
            {
                throw new ArgumentNullException(nameof(text));
            }

            if (text == "_")
            {
                return(None);
            }

            int unitIndex = 0;

            foreach (var c in text)
            {
                if (unitIndex == 0 && c == '-')
                {
                    unitIndex++;
                }
                else if (char.IsDigit(c) || c == '.')
                {
                    unitIndex++;
                }
                else
                {
                    break;
                }
            }

            if (unitIndex == text.Length)
            {
                return(new Unit(double.Parse(text), UnitType.None));
            }
            else
            {
                string number = text.Substring(0, unitIndex);

                // skip optional space
                if (text[unitIndex] == ' ')
                {
                    unitIndex++;
                }

                UnitType unitType = UnitTypeHelper.Parse(text.Substring(unitIndex));

                return(new Unit(double.Parse(number), unitType));
            }
        }