如何圆到一半,总是在积极的方向?

Sau*_*mar 2 c# vb.net math function

我如何实现以下四舍五入?

0.012608376> 0.015

2.1> 2.5

2.4> 2.5

2.5> 2.5

2.6> 3

.01> .05

Ste*_*ger 9

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)

顺便说一句,最好与小数一起使用.