Rounding up to 2 decimal places in C#

前端 未结 5 666
無奈伤痛
無奈伤痛 2020-11-29 12:01

I have a decimal number which can be like the following:

189.182

I want to round this up to 2 decimal places, so the ou

相关标签:
5条回答
  • 2020-11-29 12:29
       // Double will return the wrong value for some cases. eg: 160.80
        public static decimal  RoundUp(decimal input, int places)
        {
            decimal multiplier = Convert.ToDecimal(Math.Pow(10, Convert.ToDouble(places)));
            return Math.Ceiling(input * multiplier) / multiplier;
        }
    
    0 讨论(0)
  • 2020-11-29 12:31

    Multiply by 100, call ceiling, divide by 100 does what I think you are asking for

    public static double RoundUp(double input, int places)
    {
        double multiplier = Math.Pow(10, Convert.ToDouble(places));
        return Math.Ceiling(input * multiplier) / multiplier;
    }
    

    Usage would look like:

    RoundUp(189.182, 2);
    

    This works by shifting the decimal point right 2 places (so it is to the right of the last 8) then performing the ceiling operation, then shifting the decimal point back to its original position.

    0 讨论(0)
  • 2020-11-29 12:32
                var numberToBeRound1 = 4.125;
                var numberToBeRound2 = 4.175;
                var numberToBeRound3 = 4.631;
                var numberToBeRound4 = 4.638;
                var numberOfDecimalPlaces = 2;
                var multiplier = Math.Pow(10, numberOfDecimalPlaces);
    
                //To Round Up => 4.13
                var roundedUpNumber = Math.Ceiling(numberToBeRound1 * multiplier) / multiplier;
    
                //To Round Down => 4.12
                var roundedDownNumber = Math.Floor(numberToBeRound1 * multiplier) / multiplier;
    
                //To Round To Even => 4.12
                var roundedDownToEvenNumber = Math.Round(numberToBeRound1, numberOfDecimalPlaces, MidpointRounding.ToEven);
    
                //To Round To Even => 4.18
                var roundedUpToEvenNumber = Math.Round(numberToBeRound2, numberOfDecimalPlaces, MidpointRounding.ToEven);
    
                //To Round To Away From Zero => 4.63
                var roundedDownToAwayFromZero = Math.Round(numberToBeRound3, numberOfDecimalPlaces, MidpointRounding.AwayFromZero);
    
                //To Round To Away From Zero => 4.64
                var roundedUpToAwayFromZero2 = Math.Round(numberToBeRound4, numberOfDecimalPlaces, MidpointRounding.AwayFromZero);
    
    0 讨论(0)
  • 2020-11-29 12:39

    You can use:

    decimal n = 189.182M;
    n = System.Math.Ceiling (n * 100) / 100;
    

    An explanation of the various rounding functions can be found here.


    Be aware that formulae like this are still constrained by the limited precision of the double type, should that be the type you are using (your question stated decimal but it's possible you may just have meant a floating point value with fractional component rather than that specific type).

    For example:

    double n = 283.79;
    n = System.Math.Ceiling (n * 100);
    

    will actually give you 28380, not the 283.79 you would expect(a).

    If you want accuarate results across the board, you should definitely be using the decimal type.


    (a) This is because the most accurate IEEE754 double precision representation of 283.79 is actually:

     283.790000000000020463630789891
    

    That extra (admittedly minuscule) fractional component beyond the .79 gets ceilinged up, meaning it will give you a value higher than you would expect.

    0 讨论(0)
  • 2020-11-29 12:46

    How about

    0.01 * ceil(100 * 189.182)
    
    0 讨论(0)
提交回复
热议问题