string,object,double to int的心得2011-02-06 博客园 和风大剑师看了许多文章,很多人使用Convert.ToInt32来强制转换,但发现当字符串值以小数,科学计数法表示时候无法转换。如果在ToInt32之前先将字符串转换为Detimal就不会有这种情况。public static int Do(string value) { if (string.IsNullOrEmpty(value)) return 0;
decimal result = 0; decimal.TryParse(value, out result); return System.Convert.ToInt32(result); }我的转换类全部代码namespace CS.Convert { public class toInt { public static int Do(string value) { if (string.IsNullOrEmpty(value)) return 0;
decimal result = 0; decimal.TryParse(value, out result); return System.Convert.ToInt32(result); }
public static int Do(decimal value) { return System.Convert.ToInt32(value); }
public static int Do(float value) { return System.Convert.ToInt32(value); }
public static int Do(double value) { return System.Convert.ToInt32(value); }
public static int Do(int value) { return value; }
public static int Do(object value) { if (null == value || string.IsNullOrEmpty(value.ToString())) return 0; decimal result = 0; decimal.TryParse(value.ToString(), out result);
return System.Convert.ToInt32(result); }
public static int Do(DBNull value) { return 0; } } }