
Rounding in C#
I'd like to apologize in advance for these questions. I know they're incredibly basic.<BR><BR>Using ASP.NET with C#, can somebody please tell me how to round a result either using a ASP.NET rounding method or a C# operation (or both)? <BR><BR>For example, what is the easiest and most appropriate way to convert a decimal value of 113.9764995 as 113.98 BEFORE assigning it to my variable? <BR><BR>Also, if 113.9764995 is saved in my variable, using ASP.NET, how can I DISPLAY it as 113.98 without actually changing the value in my variable?<BR><BR>Finally, I'd be very appreciative if anybody can refer me to either a C# or ASP.NET resource (or resources) of rounding functions to perform such tasks. I've searched high and low on a number of sites for the answer to these questions, but haven't had any luck.<BR>

Well, Visual Studio says...
When I click on HELP, click on INDEX, selected "FILTERED BY C#", and type in ROUND for the search term:<BR><BR>[After I look at "round function" and realize it's inappropriate.]<BR><BR>ROUND METHOD<BR><BR>Math.Round MethodSee Also<BR>Math Class  Math Members  System Namespace  Managed Extensions for C++ Programming <BR>Language<BR>C#<BR><BR>C++<BR><BR>JScript<BR><B R>Visual Basic<BR><BR>Show All<BR>Returns the number nearest the specified value.<BR><BR>Overload List<BR>Returns the whole number nearest the specified value.<BR><BR>Supported by the .NET Compact Framework.<BR><BR>[Visual Basic] Overloads Public Shared Function Round(Decimal) As Decimal<BR>[C#] public static decimal Round(decimal);<BR>[C++] public: static Decimal Round(Decimal);<BR>[JScript] public static function Round(Decimal) : Decimal;<BR>Returns the whole number nearest the specified value.<BR><BR>Supported by the .NET Compact Framework.<BR><BR>[Visual Basic] Overloads Public Shared Function Round(Double) As Double<BR>[C#] public static double Round(double);<BR>[C++] public: static double Round(double);<BR>[JScript] public static function Round(double) : double;<BR>Returns the number with the specified precision nearest the specified value.<BR><BR>Supported by the .NET Compact Framework.<BR><BR>[Visual Basic] Overloads Public Shared Function Round(Decimal, Integer) As Decimal<BR>[C#] public static decimal Round(decimal, int);<BR>[C++] public: static Decimal Round(Decimal, int);<BR>[JScript] public static function Round(Decimal, int) : Decimal;<BR>Returns the number with the specified precision nearest the specified value.<BR><BR>Supported by the .NET Compact Framework.<BR><BR>[Visual Basic] Overloads Public Shared Function Round(Double, Integer) As Double<BR>[C#] public static double Round(double, int);<BR>[C++] public: static double Round(double, int);<BR>[JScript] public static function Round(double, int) : double;<BR><BR>****************<BR><BR>This is in the System.Math class, of course. Since the method is a static method, you have to actually specify the class name:<BR><BR>double num = 113.9764995;<BR>double roundedNum = Math.Round( num, 2 )<BR><BR>*****************<BR><BR>HAVING SAID ALL THAT...<BR><BR>If you are used to standard mathematical rounding, then this is TOTALLY BOGUS! As the docs go on to say:<BR>The behavior of this method follows IEEE Standard 754, section 4. This kind of rounding is sometimes called rounding to nearest, or banker's rounding. If digits is zero, this kind of rounding is sometimes called rounding toward zero.<BR><BR>Ugh. The example is:<BR>[C#] <BR>Math.Round(3.44, 1); //Returns 3.4.<BR>Math.Round(3.45, 1); //Returns 3.4.<BR>Math.Round(3.46, 1); //Returns 3.5.<BR><BR>I dunno about you, but I would have expected 3.45 to round to 3.5!!! Still, if this is "banker's rounding", then maybe it's what you should use if you are working with money.<BR><BR>But if you wanted the mathematicalstyle rounding...<BR><BR>You may prefer to write your own function. Pretty easy:<BR><BR>roundedNum = Math.Floor( num * 100.0 + 0.5 ) / 100.0;<BR><BR>That's for rouding to 2 digits of course. And it doesn't account for negative numbers. Turn it into a function if you want to do it right.<BR><BR><BR><BR><BR>

Thanks!!!
Unfortunately, I'm working with notepad, but with your help, I was able to find info on the MSDN site about the Math.Round function. It solved my problem.<BR><BR>Thanks very much!

What do you think...
...about that kind of rounding???<BR><BR> 3.45 >> 3.4<BR><BR>??? <BR><BR><BR><BR>
Posting Permissions
 You may not post new threads
 You may not post replies
 You may not post attachments
 You may not edit your posts

Forum Rules

