/// <summary> /// 货币值格式化为字符串,如123400格式为123,400 /// </summary> /// <param name="c">小数值</param> /// <param name="keepDecimal">是否保留小数部分</param> /// <returns></returns> public static string FormatMoney(decimal c, bool keepDecimal) { bool b = false; //是否为负数 if(c < 0) { c *= -1; b = true; } string str = c.ToString(); string decimals = String.Empty; //小数部分 if (str.IndexOf('.') > -1) { decimals = str.Split('.')[1]; str = str.Split('.')[0]; //整数部分 } //分隔符个数 int num = 0; num = str.Length / 3; if (num * 3 == str.Length) { num--; } //插入分隔符 if (str.Length % 3 == 0) { for(int i = 1, j = 0; i <= num; i++, j++) { str = str.Insert(3 * i + j, ","); } } else { int startIndex = str.Length % 3; for (int i = 0; i < num; i++) { str = str.Insert(startIndex + 3 * i + i, ","); } } if (keepDecimal) { if(decimals == String.Empty) { str += ".00"; } else { str += "." + decimals; } } return b ? "-" + str : str; }