Search All Articles Submit your Website or Blog to A New Internet Library
Friday, February 26, 2010
MS Excel like Round function in c#.
1. Excel Round function behaves differently when we compare with C# Math.Round() method.
2. Also Excel Round allows Round to -1, -2... example Round(121,-1) = 120. This is not allowed in Math.Round()
3. Say if we want to Round 1234.5678 to nearest integer value then Math.Round() returns 1234 where as MS Excel Round gives 12345.
4. Below is a C# function that gives similar result as MS Excel Round and also allows negative rounding.
public double roundNum(double num, int place)
{
double n;
n = num * Math.Pow(10, place);
n = Math.Sign(n) * Math.Abs(Math.Floor(n + .5));
return n / Math.Pow(10, place);
}
Note: Visual Studio 2005 allows us to mention Round Up or Round down for Math.Round() method.
Subscribe to:
Post Comments (Atom)
Also Read other Top Articles
- JSON Serialization in VS 2008
- Implementing Forms Authentication in Silverlight Application.
- Making GridView Rows or Individual Cells Clickable and Selectable.
- Enabling browser back button for GridView Paging and Sorting in Ajax 1.1 and 3.5 (using Visual Studio 2005/ Visual studio 2008)
- How to pass values from User Control to Page or calling Page methods from User Control.
- What is WCF?
- New features in C# 4.0
- C# to VB.NET and VB.NET to C# online free converter tools.


No comments:
Post a Comment
Post your comments/questions/feedback for this Article.