using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Utils.Datas { public class NumToStringUtil { /// /// 小数位最多N位 /// public static string GetPointAtMost(decimal num, int places = 2) { var s = GetStandardPoint(num, places); if (!s.EndsWith("0")) return s; int zeroLength = 1; for (int i = 1; i < places; i++) { if (s.ElementAt(s.Length - zeroLength - 1) != '0') break; zeroLength++; } if (zeroLength == places) return s.Substring(0, s.Length - zeroLength - 1); return s.Substring(0, s.Length - zeroLength); } /// /// N位小数 /// public static string GetStandardPoint(decimal num, int places = 2) { if (places < 1) places = 2; return num.ToString("F" + places); } } }