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
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.
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.
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.
We can use the Excel ROUND function then subtract to round price to end in . 99 value.
double ROUNDUP( double number, int digits )
{
return Math.Ceiling(number * Math.Pow(10, digits)) / Math.Pow(10, digits);
}
Math.Ceiling is what you're looking for.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With