public void RoundTripMinValueTest()
        {
            var converter = new DoubleConverter();
            var s         = converter.ConvertToString(double.MinValue, null, new MemberMapData(null));
            var d         = converter.ConvertFromString(s, null, new MemberMapData(null));

            Assert.AreEqual(double.MinValue, d);
        }
Example #2
0
        public static Unit Parse(String str)
        {
            if (str == null)
            {
                return(Unit.Empty);
            }

            str = str.Trim().ToLower(CultureInfo.InvariantCulture);
            int length      = str.Length;
            int digitLength = -1;

            for (int i = 0; i < length; i++)
            {
                char ch = str[i];
                if ((ch < '0' || ch > '9') && (ch != '-' && ch != '.' && ch != ','))
                {
                    break;
                }

                digitLength = i;
            }
            if (digitLength == -1)
            {
                // No digits in the width, we ignore this style
                return(str == "auto"? Unit.Auto : Unit.Empty);
            }

            UnitMetric type;

            if (digitLength < length - 1)
            {
                type = ConverterUtility.ConvertToUnitMetric(str.Substring(digitLength + 1).Trim());
            }
            else
            {
                type = UnitMetric.Pixel;
            }

            string v = str.Substring(0, digitLength + 1);
            double value;

            try
            {
                TypeConverter converter = new DoubleConverter();
                value = (double)converter.ConvertFromString(null, CultureInfo.InvariantCulture, v);

                if (value < Int16.MinValue || value > Int16.MaxValue)
                {
                    return(Unit.Empty);
                }
            }
            catch (FormatException)
            {
                return(Unit.Empty);
            }
            catch (ArithmeticException)
            {
                return(Unit.Empty);
            }

            return(new Unit(type, value));
        }