Rounding up to 2 decimal places in C#

前端 未结 5 728
無奈伤痛
無奈伤痛 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;
        }
    

提交回复
热议问题