public void TestToFloat() { Assert.True(Math.Abs(123 - FloatConverter.ToFloat(123)) < 0.001); Assert.True(Math.Abs(123.456 - FloatConverter.ToFloat(123.456)) < 0.001); Assert.True(Math.Abs(123.456 - FloatConverter.ToFloat("123.456")) < 0.001); Assert.True(Math.Abs(123 - FloatConverter.ToFloatWithDefault(null, 123)) < 0.001); Assert.True(Math.Abs(0 - FloatConverter.ToFloatWithDefault(false, 123)) < 0.001); Assert.True(Math.Abs(123 - FloatConverter.ToFloatWithDefault("ABC", 123)) < 0.001); }
public static TimeSpan?ToNullableTimeSpan(object value) { if (value == null) { return(null); } if (value is DateTime || value is DateTime?) { return(null); } if (value is TimeSpan || value is TimeSpan?) { return((TimeSpan)value); } if (value is int) { return(TimeSpan.FromMilliseconds((int)value)); } if (value is long) { return(TimeSpan.FromMilliseconds((long)value)); } if (value is float) { return(TimeSpan.FromMilliseconds((float)value)); } if (value is double) { return(TimeSpan.FromMilliseconds((double)value)); } try { float?millis = FloatConverter.ToNullableFloat(value); if (millis.HasValue) { return(TimeSpan.FromMilliseconds(millis.Value)); } return(null); } catch { return(null); } }
public static T ToTypeWithDefault <T>(object value, T defaultValue) { if (value == null) { return(defaultValue); } if (value is T) { return((T)value); } var typeInfo = typeof(T).GetTypeInfo(); if (typeInfo.IsEnum) { value = EnumConverter.ToEnumWithDefault <T>(value, defaultValue); } else if (typeInfo.IsAssignableFrom(typeof(decimal))) { value = DecimalConverter.ToNullableDecimal(value); } else if (typeInfo.IsAssignableFrom(typeof(string))) { value = StringConverter.ToNullableString(value); } else if (typeInfo.IsAssignableFrom(typeof(long))) { value = LongConverter.ToNullableLong(value); } else if (typeInfo.IsAssignableFrom(typeof(int))) { value = IntegerConverter.ToNullableInteger(value); } else if (typeInfo.IsAssignableFrom(typeof(double))) { value = DoubleConverter.ToNullableDouble(value); } else if (typeInfo.IsAssignableFrom(typeof(float))) { value = FloatConverter.ToNullableFloat(value); } else if (typeInfo.IsAssignableFrom(typeof(bool))) { value = BooleanConverter.ToNullableBoolean(value); } else if (typeInfo.IsAssignableFrom(typeof(DateTime))) { value = DateTimeConverter.ToNullableDateTime(value); } else if (typeInfo.IsAssignableFrom(typeof(TimeSpan))) { value = TimeSpanConverter.ToNullableTimeSpan(value); } if (value == null) { return(defaultValue); } try { if (typeInfo.IsClass || typeInfo.IsInterface) { return(value is T ? (T)value : defaultValue); } else { return((T)value); } } catch { return(defaultValue); } }
public static T?ToNullableType <T>(object value) where T : struct { if (value == null) { return(null); } if (value is T) { return((T)value); } var typeInfo = typeof(T).GetTypeInfo(); if (typeInfo.IsEnum) { value = EnumConverter.ToNullableEnum <T>(value); } else if (typeInfo.IsAssignableFrom(typeof(decimal))) { value = DecimalConverter.ToNullableDecimal(value); } else if (typeInfo.IsAssignableFrom(typeof(string))) { value = StringConverter.ToNullableString(value); } else if (typeInfo.IsAssignableFrom(typeof(long))) { value = LongConverter.ToNullableLong(value); } else if (typeInfo.IsAssignableFrom(typeof(int))) { value = IntegerConverter.ToNullableInteger(value); } else if (typeInfo.IsAssignableFrom(typeof(double))) { value = DoubleConverter.ToNullableDouble(value); } else if (typeInfo.IsAssignableFrom(typeof(float))) { value = FloatConverter.ToNullableFloat(value); } else if (typeInfo.IsAssignableFrom(typeof(DateTime))) { value = DateTimeConverter.ToNullableDateTime(value); } else if (typeInfo.IsAssignableFrom(typeof(TimeSpan))) { value = TimeSpanConverter.ToNullableTimeSpan(value); } if (value == null) { return(null); } try { return((T)value); } catch { return(null); } }