如何根据c#中的最后一位向上或向下舍入十进制值?

问题描述 投票:0回答:0

我有两个值,我想像这样四舍五入。如何使用 C# 中的数学方法做到这一点?

100.67 -> 100.7 或 100.70 -- 向上

100.67 -> 100.6 或 100.60 -- 向下

50.563 -> 50.564 -- 向上

50.563 -> 50.560 或 50.560

我试过这个代码块,但它没有像我想要的那样工作。当数字 100.67 和小数位 2 时,值保持不变。

public double RoundDown(double number, int decimalPlaces) { 返回 Math.Floor(number * Math.Pow(10, decimalPlaces)) / Math.Pow(10, decimalPlaces); }

public double RoundUp(double number, int decimalPlaces) { 返回 Math.Ceiling(number * Math.Pow(10, decimalPlaces)) / Math.Pow(10, decimalPlaces); }

c# decimal rounding calculation
© www.soinside.com 2019 - 2024. All rights reserved.