如何实现以下舍入?
0.012608376 > 0.015
2.1 > 2.5
2.4 > 2.5
2.5 > 2.5
2.6 > 3
.01 > .05
public double Round(double input, int decimalPlaces)
{
double precision = 2.0 * Math.Pow(10, decimalPlaces - 1);
// Ceiling also rounds negative values in positive direction
return Math.Ceiling(x * precision) / precision;
}
像这样使用:
Round(0.012608376, 3) returns 0.015
Round(2.1, 1) returns 2.5
Round(2.4, 1) returns 2.5
Round(2.5, 1) returns 2.5
Round(2.6, 1) returns 3
Round(.01, 2) returns .05
顺便说一句,最好与小数一起使用。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)