/// <summary> /// Gets Chinese Year<br /> /// 获得农历年 /// </summary> /// <param name="dt"></param> /// <param name="traditionalChineseCharacters"></param> /// <returns></returns> public static string GetChineseYear(DateTime dt, bool traditionalChineseCharacters = false) { var hzNumP = traditionalChineseCharacters ? HZNUMZ : HZNUMS; var year = dt.Year.ToString().ToCharArray(); var yearStr = new string[year.Length]; for (var i = 0; i < year.Length; i++) { yearStr[i] = hzNumP[NumericConv.ToInt32(year[i])]; } return($"{string.Join("", yearStr)}年"); }
public void DecimalConvertTest() { object obj1 = 64.445566; object obj2 = "64"; object obj3 = null; object obj4 = "64.445566"; object obj5 = (decimal)64.445566; object obj6 = (decimal) - 64.445566; object obj7 = "-64"; object obj8 = "-64.445566"; object obj9 = -64.445566F; string str1 = "64.445566"; string str2 = ""; string str3 = null; string str4 = "64.445566"; string str5 = "-64"; string str6 = "-64.445566"; string str7 = "64D"; NumericConv.ToDecimal(obj1).ShouldBe((decimal)64.445566); NumericConv.ToDecimal(obj2).ShouldBe((decimal)64); NumericConv.ToDecimal(obj3).ShouldBe((decimal)0); NumericConv.ToDecimal(obj4).ShouldBe((decimal)64.445566); NumericConv.ToDecimal(obj5).ShouldBe((decimal)64.445566); NumericConv.ToDecimal(obj6).ShouldBe((decimal) - 64.445566); NumericConv.ToDecimal(obj7).ShouldBe((decimal) - 64); NumericConv.ToDecimal(obj8).ShouldBe((decimal) - 64.445566); NumericConv.ToDecimal(str1).ShouldBe((decimal)64.445566); NumericConv.ToDecimal(str2).ShouldBe((decimal)0); NumericConv.ToDecimal(str3).ShouldBe((decimal)0); NumericConv.ToDecimal(str4).ShouldBe((decimal)64.445566); NumericConv.ToDecimal(str5).ShouldBe((decimal) - 64); NumericConv.ToDecimal(str6).ShouldBe((decimal) - 64.445566); NumericConv.ToDecimal(str7).ShouldBe((decimal)0); NumericConv.ToNullableDecimal(obj1).ShouldBe((decimal?)64.445566); NumericConv.ToNullableDecimal(obj2).ShouldBe((decimal?)64); NumericConv.ToNullableDecimal(obj3).ShouldBe(null); NumericConv.ToNullableDecimal(obj4).ShouldBe((decimal?)64.445566); NumericConv.ToNullableDecimal(obj5).ShouldBe((decimal?)64.445566); NumericConv.ToNullableDecimal(obj6).ShouldBe((decimal?)-64.445566); NumericConv.ToNullableDecimal(obj7).ShouldBe((decimal?)-64); NumericConv.ToNullableDecimal(obj8).ShouldBe((decimal?)-64.445566); NumericConv.ToNullableDecimal(str1).ShouldBe((decimal?)64.445566); NumericConv.ToNullableDecimal(str2).ShouldBe(null); NumericConv.ToNullableDecimal(str3).ShouldBe(null); NumericConv.ToNullableDecimal(str4).ShouldBe((decimal?)64.445566); NumericConv.ToNullableDecimal(str5).ShouldBe((decimal?)-64); NumericConv.ToNullableDecimal(str6).ShouldBe((decimal?)-64.445566); NumericConv.ToNullableDecimal(str7).ShouldBe(null); }
public void DoubleConvertTest() { object obj1 = 64; object obj2 = "64"; object obj3 = null; object obj4 = "64.1"; object obj5 = 64D; object obj6 = -64; object obj7 = "-64"; object obj8 = "-64.1"; object obj9 = -64; string str1 = "64"; string str2 = ""; string str3 = null; string str4 = "64.1"; string str5 = "-64"; string str6 = "-64.1"; string str7 = "64D"; NumericConv.ToDouble(obj1).ShouldBe(64); NumericConv.ToDouble(obj2).ShouldBe(64); NumericConv.ToDouble(obj3).ShouldBe(0); NumericConv.ToDouble(obj4).ShouldBe(64.1D); NumericConv.ToDouble(obj5).ShouldBe(64); NumericConv.ToDouble(obj6).ShouldBe(-64); NumericConv.ToDouble(obj7).ShouldBe(-64); NumericConv.ToDouble(obj8).ShouldBe(-64.1D); NumericConv.ToDouble(str1).ShouldBe(64); NumericConv.ToDouble(str2).ShouldBe(0); NumericConv.ToDouble(str3).ShouldBe(0); NumericConv.ToDouble(str4).ShouldBe(64.1D); NumericConv.ToDouble(str5).ShouldBe(-64); NumericConv.ToDouble(str6).ShouldBe(-64.1D); NumericConv.ToDouble(str7).ShouldBe(0); NumericConv.ToNullableDouble(obj1).ShouldBe((double?)64); NumericConv.ToNullableDouble(obj2).ShouldBe((double?)64); NumericConv.ToNullableDouble(obj3).ShouldBe(null); NumericConv.ToNullableDouble(obj4).ShouldBe(64.1D); NumericConv.ToNullableDouble(obj5).ShouldBe((double?)64); NumericConv.ToNullableDouble(obj6).ShouldBe((double?)-64); NumericConv.ToNullableDouble(obj7).ShouldBe((double?)-64); NumericConv.ToNullableDouble(obj8).ShouldBe(-64.1D); NumericConv.ToNullableDouble(str1).ShouldBe((double?)64); NumericConv.ToNullableDouble(str2).ShouldBe(null); NumericConv.ToNullableDouble(str3).ShouldBe(null); NumericConv.ToNullableDouble(str4).ShouldBe(64.1D); NumericConv.ToNullableDouble(str5).ShouldBe((double?)-64); NumericConv.ToNullableDouble(str6).ShouldBe(-64.1D); NumericConv.ToNullableDouble(str7).ShouldBe(null); }
public void SingleConvertTest() { object obj1 = 64; object obj2 = "64"; object obj3 = null; object obj4 = "64.1"; object obj5 = 64F; object obj6 = -64; object obj7 = "-64"; object obj8 = "-64.1"; object obj9 = -64; string str1 = "64"; string str2 = ""; string str3 = null; string str4 = "64.1"; string str5 = "-64"; string str6 = "-64.1"; string str7 = "64F"; NumericConv.ToFloat(obj1).ShouldBe(64); NumericConv.ToFloat(obj2).ShouldBe(64); NumericConv.ToFloat(obj3).ShouldBe(0); NumericConv.ToFloat(obj4).ShouldBe(64.1f); NumericConv.ToFloat(obj5).ShouldBe(64); NumericConv.ToFloat(obj6).ShouldBe(-64); NumericConv.ToFloat(obj7).ShouldBe(-64); NumericConv.ToFloat(obj8).ShouldBe(-64.1f); NumericConv.ToFloat(str1).ShouldBe(64); NumericConv.ToFloat(str2).ShouldBe(0); NumericConv.ToFloat(str3).ShouldBe(0); NumericConv.ToFloat(str4).ShouldBe(64.1f); NumericConv.ToFloat(str5).ShouldBe(-64); NumericConv.ToFloat(str6).ShouldBe(-64.1f); NumericConv.ToFloat(str7).ShouldBe(0); NumericConv.ToNullableFloat(obj1).ShouldBe((float?)64); NumericConv.ToNullableFloat(obj2).ShouldBe((float?)64); NumericConv.ToNullableFloat(obj3).ShouldBe(null); NumericConv.ToNullableFloat(obj4).ShouldBe(64.1f); NumericConv.ToNullableFloat(obj5).ShouldBe((float?)64); NumericConv.ToNullableFloat(obj6).ShouldBe((float?)-64); NumericConv.ToNullableFloat(obj7).ShouldBe((float?)-64); NumericConv.ToNullableFloat(obj8).ShouldBe(-64.1f); NumericConv.ToNullableFloat(str1).ShouldBe((float?)64); NumericConv.ToNullableFloat(str2).ShouldBe(null); NumericConv.ToNullableFloat(str3).ShouldBe(null); NumericConv.ToNullableFloat(str4).ShouldBe(64.1f); NumericConv.ToNullableFloat(str5).ShouldBe((float?)-64); NumericConv.ToNullableFloat(str6).ShouldBe(-64.1f); NumericConv.ToNullableFloat(str7).ShouldBe(null); }
public void UInt64ConvertTest() { object obj1 = 64; object obj2 = "64"; object obj3 = null; object obj4 = "64.1"; object obj5 = (ulong)64L; object obj6 = -64; object obj7 = "-64"; object obj8 = "-64.1"; object obj9 = -64; string str1 = "64"; string str2 = ""; string str3 = null; string str4 = "64.1"; string str5 = "-64"; string str6 = "-64.1"; string str7 = "64L"; NumericConv.ToUInt64(obj1).ShouldBe((ulong)64); NumericConv.ToUInt64(obj2).ShouldBe((ulong)64); NumericConv.ToUInt64(obj3).ShouldBe((ulong)0); NumericConv.ToUInt64(obj4).ShouldBe((ulong)64); NumericConv.ToUInt64(obj5).ShouldBe((ulong)64); NumericConv.ToUInt64(obj6).ShouldBe((ulong)0); NumericConv.ToUInt64(obj7).ShouldBe((ulong)0); NumericConv.ToUInt64(obj8).ShouldBe((ulong)0); NumericConv.ToUInt64(str1).ShouldBe((ulong)64); NumericConv.ToUInt64(str2).ShouldBe((ulong)0); NumericConv.ToUInt64(str3).ShouldBe((ulong)0); NumericConv.ToUInt64(str4).ShouldBe((ulong)64); NumericConv.ToUInt64(str5).ShouldBe((ulong)0); NumericConv.ToUInt64(str6).ShouldBe((ulong)0); NumericConv.ToUInt64(str7).ShouldBe((ulong)0); NumericConv.ToNullableUInt64(obj1).ShouldBe((ulong?)64uL); NumericConv.ToNullableUInt64(obj2).ShouldBe((ulong?)64uL); NumericConv.ToNullableUInt64(obj3).ShouldBe(null); NumericConv.ToNullableUInt64(obj4).ShouldBe(null); NumericConv.ToNullableUInt64(obj5).ShouldBe((ulong?)64uL); NumericConv.ToNullableUInt64(obj6).ShouldBe(null); NumericConv.ToNullableUInt64(obj7).ShouldBe(null); NumericConv.ToNullableUInt64(obj8).ShouldBe(null); NumericConv.ToNullableUInt64(str1).ShouldBe((ulong?)64uL); NumericConv.ToNullableUInt64(str2).ShouldBe(null); NumericConv.ToNullableUInt64(str3).ShouldBe(null); NumericConv.ToNullableUInt64(str4).ShouldBe(null); NumericConv.ToNullableUInt64(str5).ShouldBe(null); NumericConv.ToNullableUInt64(str6).ShouldBe(null); NumericConv.ToNullableUInt64(str7).ShouldBe(null); }
public void Int32ConvertTest() { object obj1 = 32; object obj2 = "32"; object obj3 = null; object obj4 = "32.1"; object obj5 = 32; object obj6 = -32; object obj7 = "-32"; object obj8 = "-32.1"; object obj9 = -32; string str1 = "32"; string str2 = ""; string str3 = null; string str4 = "32.1"; string str5 = "-32"; string str6 = "-32.1"; NumericConv.ToInt32(obj1).ShouldBe(32); NumericConv.ToInt32(obj2).ShouldBe(32); NumericConv.ToInt32(obj3).ShouldBe(0); NumericConv.ToInt32(obj4).ShouldBe(32); NumericConv.ToInt32(obj5).ShouldBe(32); NumericConv.ToInt32(obj6).ShouldBe(-32); NumericConv.ToInt32(obj7).ShouldBe(-32); NumericConv.ToInt32(obj8).ShouldBe(-32); NumericConv.ToInt32(str1).ShouldBe(32); NumericConv.ToInt32(str2).ShouldBe(0); NumericConv.ToInt32(str3).ShouldBe(0); NumericConv.ToInt32(str4).ShouldBe(32); NumericConv.ToInt32(str5).ShouldBe(-32); NumericConv.ToInt32(str6).ShouldBe(-32); NumericConv.ToNullableInt32(obj1).ShouldBe((int?)32); NumericConv.ToNullableInt32(obj2).ShouldBe((int?)32); NumericConv.ToNullableInt32(obj3).ShouldBe(null); NumericConv.ToNullableInt32(obj4).ShouldBe(null); NumericConv.ToNullableInt32(obj5).ShouldBe((int?)32); NumericConv.ToNullableInt32(obj6).ShouldBe((int?)-32); NumericConv.ToNullableInt32(obj7).ShouldBe((int?)-32); NumericConv.ToNullableInt32(obj8).ShouldBe(null); NumericConv.ToNullableInt32(str1).ShouldBe((int?)32); NumericConv.ToNullableInt32(str2).ShouldBe(null); NumericConv.ToNullableInt32(str3).ShouldBe(null); NumericConv.ToNullableInt32(str4).ShouldBe(null); NumericConv.ToNullableInt32(str5).ShouldBe((int?)-32); NumericConv.ToNullableInt32(str6).ShouldBe(null); }
public void UInt16ConvertTest() { object obj1 = 16; object obj2 = "16"; object obj3 = null; object obj4 = "16.1"; object obj5 = (ushort)16; object obj6 = -16; object obj7 = "-16"; object obj8 = "-16.1"; object obj9 = (short)-16; string str1 = "16"; string str2 = ""; string str3 = null; string str4 = "16.1"; string str5 = "-16"; string str6 = "-16.1"; NumericConv.ToUInt16(obj1).ShouldBe((ushort)16); NumericConv.ToUInt16(obj2).ShouldBe((ushort)16); NumericConv.ToUInt16(obj3).ShouldBe((ushort)0); NumericConv.ToUInt16(obj4).ShouldBe((ushort)16); NumericConv.ToUInt16(obj5).ShouldBe((ushort)16); NumericConv.ToUInt16(obj6).ShouldBe((ushort)0); NumericConv.ToUInt16(obj7).ShouldBe((ushort)0); NumericConv.ToUInt16(obj8).ShouldBe((ushort)0); NumericConv.ToUInt16(str1).ShouldBe((ushort)16); NumericConv.ToUInt16(str2).ShouldBe((ushort)0); NumericConv.ToUInt16(str3).ShouldBe((ushort)0); NumericConv.ToUInt16(str4).ShouldBe((ushort)16); NumericConv.ToUInt16(str5).ShouldBe((ushort)0); NumericConv.ToUInt16(str6).ShouldBe((ushort)0); NumericConv.ToNullableUInt16(obj1).ShouldBe((ushort?)16); NumericConv.ToNullableUInt16(obj2).ShouldBe((ushort?)16); NumericConv.ToNullableUInt16(obj3).ShouldBe(null); NumericConv.ToNullableUInt16(obj4).ShouldBe(null); NumericConv.ToNullableUInt16(obj5).ShouldBe((ushort?)16); NumericConv.ToNullableUInt16(obj6).ShouldBe(null); NumericConv.ToNullableUInt16(obj7).ShouldBe(null); NumericConv.ToNullableUInt16(obj8).ShouldBe(null); NumericConv.ToNullableUInt16(str1).ShouldBe((ushort?)16); NumericConv.ToNullableUInt16(str2).ShouldBe(null); NumericConv.ToNullableUInt16(str3).ShouldBe(null); NumericConv.ToNullableUInt16(str4).ShouldBe(null); NumericConv.ToNullableUInt16(str5).ShouldBe(null); NumericConv.ToNullableUInt16(str6).ShouldBe(null); }
public void StringValConvertToByteTest() { string val0 = "0"; string val1 = "1"; string val2 = "-1"; string val3 = "+1"; string val4 = "+0"; string val5 = "-0"; string valA = ""; string valB = null; string ValC = "0.1"; string ValD = "A"; NumericConv.ToByte(val0).ShouldBe((byte)0); NumericConv.ToByte(val1).ShouldBe((byte)1); NumericConv.ToByte(val2).ShouldBe(default);
/// <summary> /// Cast TEnum to <see cref="ushort"/> /// </summary> /// <param name="enum"></param> /// <param name="defaultVal"></param> /// <typeparam name="TEnum"></typeparam> /// <returns></returns> public static ushort CastToUShort <TEnum>(this TEnum @enum, ushort defaultVal = default) where TEnum : struct => NumericConv.EnumToUInt16(@enum, defaultVal);
/// <summary> /// Cast <see cref="string"/> to <see cref="decimal"/> /// </summary> /// <param name="str"></param> /// <param name="impls"></param> /// <returns></returns> public static decimal CastToDecimal(this string str, params IConversionImpl <string, decimal>[] impls) => NumericConv.StringToDecimal(str, impls);
/// <summary> /// Cast <see cref="string"/> to <see cref="double"/> /// </summary> /// <param name="str"></param> /// <param name="impls"></param> /// <returns></returns> public static double CastToDouble(this string str, params IConversionImpl <string, double>[] impls) => NumericConv.StringToDouble(str, impls);
/// <summary> /// Cast <see cref="string"/> to <see cref="float"/> /// </summary> /// <param name="str"></param> /// <param name="impls"></param> /// <returns></returns> public static float CastToFloat(this string str, params IConversionImpl <string, float>[] impls) => NumericConv.StringToFloat(str, impls);
/// <summary> /// Cast TEnum to <see cref="ulong"/> /// </summary> /// <param name="enum"></param> /// <param name="defaultVal"></param> /// <typeparam name="TEnum"></typeparam> /// <returns></returns> public static ulong CastToUInt64 <TEnum>(this TEnum @enum, ulong defaultVal = default) where TEnum : struct => NumericConv.EnumToUInt64(@enum, defaultVal);
/// <summary> /// Cast <see cref="string"/> to <see cref="ulong"/> /// </summary> /// <param name="str"></param> /// <param name="defaultVal"></param> /// <returns></returns> public static ulong CastToUInt64(this string str, ulong defaultVal = default) => NumericConv.StringToUInt64(str, defaultVal);
/// <summary> /// Convert <see cref="object"/> to <see cref="ushort"/>. /// </summary> /// <param name="obj"></param> /// <param name="defaultVal"></param> /// <returns></returns> public static ushort ToUInt16(object obj, ushort defaultVal = default) => NumericConv.ObjectToUInt16(obj, defaultVal);
/// <summary> /// Cast TEnum to <see cref="uint"/> /// </summary> /// <param name="enum"></param> /// <param name="defaultVal"></param> /// <typeparam name="TEnum"></typeparam> /// <returns></returns> public static uint CastToUInt32 <TEnum>(this TEnum @enum, uint defaultVal = default) where TEnum : struct => NumericConv.EnumToUInt32(@enum, defaultVal);
/// <summary> /// Cast <see cref="string"/> to <see cref="long"/> /// </summary> /// <param name="str"></param> /// <param name="defaultVal"></param> /// <returns></returns> public static long CastToLong(this string str, long defaultVal = default) => NumericConv.StringToInt64(str, defaultVal);
/// <summary> /// Convert <see cref="object"/> to nullable <see cref="ushort"/>. /// </summary> /// <param name="obj"></param> /// <returns></returns> public static ushort?ToNullableUInt16(object obj) => NumericConv.ObjectToNullableUInt16(obj);
/// <summary> /// Cast <see cref="string"/> to <see cref="ulong"/> /// </summary> /// <param name="str"></param> /// <param name="impls"></param> /// <returns></returns> public static ulong CastToUInt64(this string str, params IConversionImpl <string, ulong>[] impls) => NumericConv.StringToUInt64(str, impls);
/// <summary> /// Convert <see cref="string"/> to nullable <see cref="ushort"/>. /// </summary> /// <param name="str"></param> /// <returns></returns> public static ushort?ToNullableUInt16(string str) => NumericConv.StringToNullableUInt16(str);
/// <summary> /// Cast <see cref="string"/> to <see cref="float"/> /// </summary> /// <param name="str"></param> /// <param name="defaultVal"></param> /// <returns></returns> public static float CastToFloat(this string str, float defaultVal = default) => NumericConv.StringToFloat(str, defaultVal);
/// <summary> /// Convert <see cref="object"/> to <see cref="int"/>. /// </summary> /// <param name="obj"></param> /// <param name="defaultVal"></param> /// <returns></returns> public static int ToInt32(object obj, int defaultVal = default) => NumericConv.ObjectToInt32(obj, defaultVal);
/// <summary> /// Cast <see cref="string"/> to <see cref="double"/> /// </summary> /// <param name="str"></param> /// <param name="defaultVal"></param> /// <returns></returns> public static double CastToDouble(this string str, double defaultVal = default) => NumericConv.StringToDouble(str, defaultVal);
/// <summary> /// Convert <see cref="string"/> to <see cref="int"/>. /// </summary> /// <param name="str"></param> /// <param name="defaultVal"></param> /// <returns></returns> public static int ToInt32(string str, int defaultVal = default) => NumericConv.StringToInt32(str, defaultVal);
/// <summary> /// Cast <see cref="string"/> to <see cref="decimal"/> /// </summary> /// <param name="str"></param> /// <param name="defaultVal"></param> /// <returns></returns> public static decimal CastToDecimal(this string str, decimal defaultVal = default) => NumericConv.StringToDecimal(str, defaultVal);
/// <summary> /// Cast <see cref="string"/> to <see cref="short"/> /// </summary> /// <param name="str"></param> /// <param name="defaultVal"></param> /// <returns></returns> public static short CastToShort(this string str, short defaultVal = default) => NumericConv.StringToInt16(str, defaultVal);
/// <summary> /// Cast <see cref="string"/> to <see cref="ushort"/> /// </summary> /// <param name="str"></param> /// <param name="impls"></param> /// <returns></returns> public static ushort CastToUShort(this string str, params IConversionImpl <string, ushort>[] impls) => NumericConv.StringToUInt16(str, impls);
/// <summary> /// Cast <see cref="string"/> to <see cref="uint"/> /// </summary> /// <param name="str"></param> /// <param name="defaultVal"></param> /// <returns></returns> public static uint CastToUInt32(this string str, uint defaultVal = default) => NumericConv.StringToUInt32(str, defaultVal);
/// <summary> /// Cast <see cref="string"/> to <see cref="ushort"/> /// </summary> /// <param name="str"></param> /// <param name="defaultVal"></param> /// <returns></returns> public static ushort CastToUInt16(this string str, ushort defaultVal = default) => NumericConv.StringToUInt16(str, defaultVal);
/// <summary> /// Cast <see cref="string"/> to <see cref="uint"/> /// </summary> /// <param name="str"></param> /// <param name="impls"></param> /// <returns></returns> public static uint CastToUInt32(this string str, params IConversionImpl <string, uint>[] impls) => NumericConv.StringToUInt32(str, impls);