/// <summary> /// 根據精度將金額轉換為自定義類型的string /// </summary> /// <param name="precision">需要的精度</param> /// <param name="money">需要轉換的金額</param> /// <returns>轉換後的金額</returns> public string FormateString(int precision, double money) { //比如,FormateString(2,2333)==>結果:2,333.00 string format = "###,###,###,##0."; for (int i = 0; i < precision; i++) { format = format + "0"; } return money.ToString(format); } /// <summary> /// 轉換任意類型的對象為Double /// </summary> /// <param name="val">需要轉換的對象</param> /// <returns>返回Double類型的變量</returns> public double ConvertDouble(object val) { if (((val == null) || (val.ToString() == "")) || (val is DBNull)) { return 0.0; } if (val is string) { val = val.ToString().Replace(",", ""); } try { return Convert.ToDouble(val); } catch { return 0.0; } }
本文URL:http://www.bianceng.cn/Programming/csharp/201410/45582.htm