Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Excel RoundUp vs .NET Math.Round

in Excel, =ROUNDUP(474.872126666666, 2) -> 474.88
in .NET,

Math.Round(474.87212666666666666666666666667, 2, MidpointRounding.ToEven) // 474.87
Math.Round(474.87212666666666666666666666667, 2, MidpointRounding.AwayFromZero) // 474.87

My client want Excel rounding result, is there any way I can get 474.88 in .NET?

Thanks a lot

like image 698
nandin Avatar asked Jun 30 '09 20:06

nandin


People also ask

What is the difference between Roundup and round in Excel?

Below you will find a list of functions specially designed for performing different types of rounding in Excel. ROUND - round the number to the specified number of digits. ROUNDUP - round the number upward to the specified number of digits. ROUNDDOWN - round the number downward to the specified number of digits.

Is there a roundup function in Excel?

Remarks. ROUNDUP behaves like ROUND, except that it always rounds a number up. If num_digits is greater than 0 (zero), then number is rounded up to the specified number of decimal places. If num_digits is 0, then number is rounded up to the nearest integer.

Does 0.5 round up or down in Excel?

Excel has a very elementary take on rounding. It's the same method you learned early in grade school: If you're rounding to a whole number, decimals from 0.1 to 0.4 round down and decimals from 0.5 to 0.9 round up.

How do I round to .99 in Excel?

We can use the Excel ROUND function then subtract to round price to end in . 99 value.


2 Answers

double ROUNDUP( double number, int digits )
  {
     return Math.Ceiling(number * Math.Pow(10, digits)) / Math.Pow(10, digits);
  }
like image 69
msergeant Avatar answered Oct 24 '22 19:10

msergeant


Math.Ceiling is what you're looking for.

like image 43
MiffTheFox Avatar answered Oct 24 '22 19:10

MiffTheFox