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;
}
Run Code Online (Sandbox Code Playgroud)
使用这样:
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
Run Code Online (Sandbox Code Playgroud)
顺便说一句,最好与小数一起使用.