↑ Grab this Headline Animator

Friday, February 26, 2010

MS Excel like Ceiling function in c#.

- EXcel Ceiling function returns a number rounded up based on a multiple of significance.
- Example Ceiling(41.88, 10) will give 50 and Ceiling of 41.88 to 5 will give 45.
- Below is the C# function which gives the same result as Ceiling in MS Excel.

public int Ceiling(double input, int ceilTo)
if ((input % ceilTo) != 0)
return ((int)(input / ceilTo) * ceilTo) + ceilTo;
return Convert.ToInt32(input);

Submit this story to DotNetKicks

1 comment:

Anonymous said...

The above function i have tried it doesn't work

I have tried by making changes to it and it worked exactly like the Excel Ceiling

Public Function Ceiling(ByVal input As Double, ByVal ceilTo As Integer) As Integer
If (input Mod ceilTo) <> 0 Then
Return CInt(Math.Ceiling(input / ceilTo) * ceilTo)
Return Convert.ToInt32(input)
End If
End Function

Post a Comment

Post your comments/questions/feedback for this Article.


Latest Articles