General Topics include: .NET Framework
Note: Most posts can be categorized into one of the forums below, where experts on that topic will be able to help you find a solution to your problem faster and more efficiently.

Go Back  Xtreme .NET Talk > .NET > General > VB.NET's Math.Round()


Reply
 
Thread Tools Display Modes
  #1  
Old 07-08-2004, 10:43 AM
DeFlipMode DeFlipMode is offline
Newcomer
 
Join Date: Jul 2004
Posts: 3
DeFlipMode is on a distinguished road
Question VB.NET's Math.Round()



VB.NET's Math.Round() seems to be a Round_Half_Even as the data below shows: (5.5 rounds to 6.0, and 6.5 rounds to 6.0)

?math.round(5.2)
5.0
?math.round(5.5)
6.0
?math.round(5.8)
6.0
?math.round(6.0)
6.0
?math.round(6.5)
6.0

Is there an easy way to create a Round_Half_Up so that 5.5 rounds to 6.0 and 6.5 rounds to 7.0?

I can think of a number of ways to do this by testing, but not sure if .NET has this built in and I just can't find it in the documentaion. Any Ideas?
Reply With Quote
  #2  
Old 07-08-2004, 12:22 PM
Iceplug's Avatar
Iceplug Iceplug is offline
Contributor

Preferred language:
VB.NET, C#, J#
 
Join Date: Aug 2001
Location: California, USA
Posts: 709
Iceplug is on a distinguished road
Default

The only way that I can think of is this:
?Math.Floor(N + 0.5)

The rounding thing has something to do with statistical accuracy... The average of 5.5 and 6.5 is 6, rounding both numbers up will give you 6.5 as the average, but rounding towards even, as the .Round function, and most computer rounding algorithms, wil give you the average of 6, and similarly, you get 5 from 4.5 and 5.5 .
__________________

Iceplug, USN
One of my coworkers thinks that I believe that drawing bullets is the most efficient way of drawing bullets. Whatever!!! :-(
Reply With Quote
  #3  
Old 07-08-2004, 12:33 PM
DeFlipMode DeFlipMode is offline
Newcomer
 
Join Date: Jul 2004
Posts: 3
DeFlipMode is on a distinguished road
Thumbs up

Thanks!!

Looking at your method makes me think... Duh... why didn't I think of that!

That is a lot easier than the method I was going to use, and more accurate. Glad I asked.
Reply With Quote
  #4  
Old 07-08-2004, 03:28 PM
Nerseus's Avatar
Nerseus Nerseus is offline
Danner

Preferred language:
C#
 
Join Date: Oct 2002
Location: Arizona, USA
Posts: 2,547
Nerseus is on a distinguished road
Default

If you need to worry about negative numbers, you'll need to use Ceiling. Here's a function I use which takes a decimal. Feel free to change it for use with double or other types.
Code:
public static decimal RoundUpToWholeNumber(decimal val)
{
    if(val<decimal.Zero)
        return (decimal)Math.Ceiling((double)val - 0.5);
    else
        return (decimal)Math.Floor((double)val + 0.5);

}

-nerseus
__________________
"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Reply With Quote
  #5  
Old 07-09-2004, 12:06 PM
DeFlipMode DeFlipMode is offline
Newcomer
 
Join Date: Jul 2004
Posts: 3
DeFlipMode is on a distinguished road
Wink

Thanks for the additional info! For the behavior I required, the Math.Floor(N+0.5) tested fine for both positive and negative numbers.... but always nice to know more than you need.

Later,
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
VS.NET's Start Page does not update joe_pool_is General 0 05-25-2005 09:27 AM
Round a number in a label Lanc1988 General 1 12-25-2004 06:21 PM
round stripes Visual Basic .NET 3 11-03-2002 07:55 PM
Math.Round Problem Fritz General 5 10-22-2002 04:44 PM

Advertisement:







Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe