deciaml percent = 0.123456789123456789M; //12345.ToString("p"); 生成 1,234,500.00% string q1 = percent.ToString("P"); //12.35%;自动四舍五入,百分数,千位分隔,自动两个小数 string q2 = percent.ToString("P0"); //12%;P后边跟数字,代表精度。 string q3 = percent.ToString("P1"); //12.3%; string q4 = percent.ToString("P2"); //12.35%; string q5 = percent.ToString("P3"); string q6 = percent.ToString("P4"); string q7 = percent.ToString("P5"); string q8 = percent.ToString("P6");
当然你还可以用一下方式:
decimal percent =0.24015M; string q9 = percent.ToString("0.##%"); //得到 "24.02%" //##个数表示保留的小数位数(必要时四舍五入)
以下为我写的非四舍五入方法:
处理了0.03 要显示为3% 的问题
#region 小数变百分比 /// <summary> /// 小数变百分比 /// </summary> /// <param name="temp">要转化为百分比的原数字</param> /// <param name="length">小数点后有几位数字</param> /// <returns></returns> public static string GetPercent(decimal Number) { int length = GetPointNumber(Number); if (length >= 2) { string temp = "P" + (length - 2).ToString(); return Number.ToString(temp); } else return ((int)(Number*100)).ToString() + "%"; } #endregion #region 获取小数点后有几位小数 public static int GetPointNumber(decimal Number) { var s = Number.ToString(); return s.Length - s.IndexOf('.') - 1; } #endregion