1

I am building an Application based on a excel sheet, at a certain point the sheet uses =ROUNDUP(701.25;-1) which is returned as 710... how do we do this in C#

I tried Math.Round that returns 701, If I try to use Math.Round() with -1 then i get this

Rounding digits must be between 0 and 15, inclusive.

Please help me out here.

Hemant.Gupta
  • 147
  • 1
  • 13

2 Answers2

2

Try this (you'll need to cast the return value to int if necessary):

public static double RoundUp(double value, int digits)
{
    double pow = Math.Pow(10, digits);
    return Math.Ceiling(value * pow) / pow;
}

This should give you the functionality defined here:

http://office.microsoft.com/en-gb/excel-help/roundup-HP005209242.aspx

Matthew Watson
  • 104,400
  • 10
  • 158
  • 276
  • `decimal calvalue = 701.25; calvalue = calvalue / 10; calvalue = Math.Ceiling(calvalue) * 10;` I am using this much less complicated, what do you think @matthew and @Thamotharan – Hemant.Gupta Aug 27 '13 at 04:23
1

use the below two methods.Which may help you

int RoundUp(int toRound)
{
     return (10 - toRound % 10) + toRound;
}

int RoundDown(int toRound)
{
    return toRound - toRound % 10;
}

Referenece Rounding integers to nearest multiple of 10

Community
  • 1
  • 1
Thamotharan Karuppiah
  • 1,755
  • 1
  • 11
  • 14