public void TestDecimalConversion() { var converter = new DecimalConverter(); Assert.True(Math.Abs((decimal)converter.ConvertFromString("123.33", CULTURE, typeof(decimal)) - ((decimal)123.33)) < (decimal)0.001); Assert.True(Math.Abs((decimal)converter.ConvertFromString("0.0000002347", CULTURE, typeof(decimal)) - ((decimal)0.0000002347)) < (decimal)0.00000000001); Assert.True(Math.Abs((decimal)converter.ConvertFromString("-0.0000002347", CULTURE, typeof(decimal)) - ((decimal) - 0.0000002347)) < (decimal)0.00000000001); Assert.True(Math.Abs((decimal)converter.ConvertFromString("-987347.2304757", CULTURE, typeof(decimal)) - ((decimal) - 987347.2304757)) < (decimal)0.00000001); Assert.True(Math.Abs((decimal)converter.ConvertFromString("123.33", CULTURE, typeof(decimal?)) - ((decimal)123.33)) < (decimal)0.001); Assert.True(Math.Abs((decimal)converter.ConvertFromString("0.0000002347", CULTURE, typeof(decimal?)) - ((decimal)0.0000002347)) < (decimal)0.00000000001); Assert.True(Math.Abs((decimal)converter.ConvertFromString("-0.0000002347", CULTURE, typeof(decimal?)) - ((decimal) - 0.0000002347)) < (decimal)0.00000000001); Assert.True(Math.Abs((decimal)converter.ConvertFromString("-987347.2304757", CULTURE, typeof(decimal?)) - ((decimal) - 987347.2304757)) < (decimal)0.00000001); Assert.True(Math.Abs((decimal)converter.ConvertFromString("1.227e-4", CULTURE, typeof(decimal)) - ((decimal)1.227e-4)) < (decimal)0.00000001); Assert.AreEqual((decimal)0, converter.ConvertFromString("0", CULTURE, typeof(decimal))); Assert.AreEqual((decimal)0, converter.ConvertFromString("0", CULTURE, typeof(decimal?))); Assert.Null(converter.ConvertFromString("", CULTURE, typeof(decimal?))); Assert.Throws <FormatException>(() => converter.ConvertFromString("", CULTURE, typeof(decimal))); }
/// <summary> /// Возвращает один из предустановленных конвертеров для преобразования значений типа <paramref name="type"/> из строк. /// </summary> /// <param name="type">Тип значения для преобразования.</param> /// <param name="format">Используемый формат преобразования. Если не задан, используется формат по умолчанию.</param> public static IConverter GetConverterForType(Type type, string format = "") { type = Nullable.GetUnderlyingType(type) ?? type; if (BooleanConverter.CanConvertTo(type)) { return(new BooleanConverter()); } if (TransparentConverter.CanConvertTo(type)) { return(TransparentConverter.Instance); } if (NumberConverter.CanConvertTo(type)) { return(new NumberConverter()); } if (DecimalConverter.CanConvertTo(type)) { return(new DecimalConverter()); } if (TimeSpanConverter.CanConvertTo(type)) { return(new TimeSpanConverter(format)); } if (DateTimeConverter.CanConvertTo(type)) { return(new DateTimeConverter(format)); } if (EnumConverter.CanConvertTo(type)) { return(new EnumConverter()); } if (CollectionConverter.CanConvertFrom(type)) { return(new CollectionConverter(GetConverterForType(CollectionConverter.GetElementType(type), format))); } throw new InvalidOperationException("failed to find suitable converter for type " + type); }