现在的位置: 首页 > 综合 > 正文

C#保留N位有效数字的方法

2018年01月23日 ⁄ 综合 ⁄ 共 330字 ⁄ 字号 评论关闭
//源自于一个大神级回复 
public static double foo(double d, int n) { if (d == 0.0) return 0; if (d > 1 || d < -1) n = n - (int)Math.Log10(Math.Abs(d)) - 1; else n = n + (int)Math.Log10(1.0 / Math.Abs(d)); if (n < 0) { d = (int)(d / Math.Pow(10, 0 - n)) * Math.Pow(10, 0 - n); n = 0; } return Math.Round(d, n); }

抱歉!评论已关闭.