C#去掉数字中无效的0,比如186.300 后面的两个0是无效的,可以转换为186.3
具体方法如下,直接上代码:
/// <summary> /// 去除数字中无效的0 如186.300 --> 186.3 /// </summary> /// <param name="src"></param> /// <returns></returns> public static string RemoveZero(string src) { if (string.IsNullOrEmpty(src)) return string.Empty; if (src.Contains(".")) { string little = src.Split('.')[1]; for (int i = little.Length - 1; i >= 0; i--) { if (little[i] != '0') { return src.Split('.')[0] + "." + little; } little = little.Remove(i, 1); } return src.Split('.')[0]; } if (src.Trim() == "-") { return null; } return src; }