如何在C#中向上或向下舍入?

时间:2012-11-20 21:07:19

标签: c# math

我尝试过使用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位。

9 个答案:

答案 0 :(得分:39)

尝试使用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应该是您要保留的位数,而不是小数位总数等。

最后,如果你想强制向上舍入(截断实际上是强制向下舍入),你只需在再次分割之前将Truncate()调用的结果加1即可。

答案 1 :(得分:23)

尝试使用Math.Ceiling(向上)或Math.Floor(向下)。例如Math.Floor(1.8) == 1.

答案 2 :(得分:14)

假设您使用的是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

答案 3 :(得分:7)

对于接受答案的较短版本,以下是可以使用的RoundUpRoundDown函数:

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);
}

答案 4 :(得分:1)

完整的代码与结果。

  double a = Math.Round(128.5, 0, MidpointRounding.AwayFromZero);

结果 129

答案 5 :(得分:1)

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 Class

Math.Pow Method (Double, Double)

Math.Floor Method (Decimal)

Math.Floor Method (Double)

Math.Ceiling Method (Decimal)

Math.Ceiling Method (Double)

答案 6 :(得分:0)

也许这个?

Math.Round(dec + 0.5m, MidpointRounding.AwayFromZero);

答案 7 :(得分:0)

尝试此自定义舍入

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);
}

答案 8 :(得分:0)

您可以通过使用Math.Round()或decimal.Round()-方法来实现:

Math.Round(amt)
Math.Round(amt, Int32) and other overloading methods.


decimal.Round(amt)
decimal.Round(amt, 2) and other overloding methods.
相关问题