如何在C#中向上或向下舍入?
我尝试过使用Math.Round和MidpointRounding。 这似乎没有做我需要的。
例:
52.34567 rounded to 2 decimals UP = 52.35 1.183 rounded to 2 decimals DOWN = 1.18
我需要编写自定义函数吗?
编辑:
我应该更具体一点。
有时我需要一个像23.567这样的数字来向下舍入到23.56。 在这种情况下……
Math.Round(dec, 2, MidpointRounding.AwayFromZero) gives 23.57 Math.Round(dec, 2, MidpointRounding.ToEven) gives 23.57
可能会出现最多9个小数位的小数,需要四舍五入到小数点后的1,2,3或甚至4位。
尝试使用decimal.Round():
decimal.Round(x, 2)
其中x
是您的值,2是您希望保留的小数位数。
您还可以通过传递第三个参数来指定.5向上或向下舍入:
decimal.Round(x, 2, MidpointRounding.AwayFromZero);
编辑:
根据新的要求(即,尽管数量大于下一个间隔的“中途”,数字有时会向下舍入),您可以尝试:
var pow = Math.Pow(10, numDigits); var truncated = Math.Truncate(x*pow) / pow;
Truncate()会停止小数的非整数部分。 请注意,上面的numDigits
应该是您要numDigits
位数,而不是小数总数等。
最后,如果你想强制向上舍入(截断实际上是强制向下舍入),你只需要在再次分割之前将Truncate()
调用的结果加1。
尝试使用Math.Ceiling
(向上)或Math.Floor
(向下)。 例如Math.Floor(1.8) == 1.
假设您使用decimal
类型的数字,
static class Rounding { public static decimal RoundUp(decimal number, int places) { decimal factor = RoundFactor(places); number *= factor; number = Math.Ceiling(number); number /= factor; return number; } public static decimal RoundDown(decimal number, int places) { decimal factor = RoundFactor(places); number *= factor; number = Math.Floor(number); number /= factor; return number; } internal static decimal RoundFactor(int places) { decimal factor = 1m; if (places < 0) { places = -places; for (int i = 0; i < places; i++) factor /= 10m; } else { for (int i = 0; i < places; i++) factor *= 10m; } return factor; } }
例:
Rounding.RoundDown(23.567, 2) prints 23.56
对于接受答案的较短版本,以下是可以使用的RoundUp
和RoundDown
函数:
public double RoundDown(double number, int decimalPlaces) { return Math.Floor(number * Math.Pow(10, decimalPlaces)) / Math.Pow(10, decimalPlaces); } public double RoundUp(double number, int decimalPlaces) { return Math.Ceiling(number * Math.Pow(10, decimalPlaces)) / Math.Pow(10, decimalPlaces); }
Math
类为您提供了用于向上和向下舍入的方法,它们分别是Math.Ceiling()
和Math.Floor()
。 它们像Math.Round()
一样工作,但它们有一个特殊性,它们只接收一个值并将它们四舍五入到整个部分。
所以你需要使用Math.Pow()
将值乘以你需要舍入功率的n个原始单位,然后你需要除以相同的乘法值。
重要的是要注意, Math.Pow()
方法的输入参数是double
,因此您需要将它们转换为double
。
例如:
如果要将值向上舍入为3位小数(假设值类型为
decimal
):double decimalsNumber = 3; decimal valueToRound = 1.1835675M; // powerOfTen must be equal to 10^3 or 1000. double powerOfTen = Math.Pow(10, decimalsNumber); // rounded must be equal to Math.Ceiling(1.1835675 * 1000) / 1000 decimal rounded = Math.Ceiling(valueToRound * (decimal)powerOfTen) / (decimal)powerOfTen; Result: rounded = 1.184
如果要将值向下舍入为3位小数(假设值类型为
decimal
):double decimalsNumber = 3; decimal valueToRound = 1.1835675M; // powerOfTen must be equal to 10^3 or 1000. double powerOfTen = Math.Pow(10, decimalsNumber); // rounded must be equal to Math.Floor(1.1835675 * 1000) / 1000 decimal rounded = Math.Floor(valueToRound * (decimal)powerOfTen) / (decimal)powerOfTen; Result: rounded = 1.183
若要更具体地参考如何使用它们并获取更多信息和两种方法,您可以从官方MSDN Microsoft站点中查看这些页面:
数学课
Math.Pow方法(双,双)
Math.Floor方法(十进制)
Math.Floor方法(双)
Math.Ceiling方法(十进制)
Math.Ceiling方法(双)
试试这个自定义舍入
public int Round(double value) { double decimalpoints = Math.Abs(value - Math.Floor(value)); if (decimalpoints > 0.5) return (int)Math.Round(value); else return (int)Math.Floor(value); }
也许这个?
Math.Round(dec + 0.5m, MidpointRounding.AwayFromZero);
完整的代码与结果。
double a = Math.Round(128.5, 0, MidpointRounding.AwayFromZero);
结果是 129