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.

Enjoy an ad free experience by logging in. Not a member yet? Register.
Go Back  Xtreme .NET Talk > .NET > General > VB.NET's Math.Round()

Thread Tools Display Modes
Old 07-08-2004, 09:43 AM
DeFlipMode DeFlipMode is offline
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)


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
Old 07-08-2004, 11:22 AM
Iceplug's Avatar
Iceplug Iceplug is offline

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

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
Old 07-08-2004, 11:33 AM
DeFlipMode DeFlipMode is offline
Join Date: Jul 2004
Posts: 3
DeFlipMode is on a distinguished road
Thumbs up


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
Old 07-08-2004, 02:28 PM
Nerseus's Avatar
Nerseus Nerseus is offline

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

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.
public static decimal RoundUpToWholeNumber(decimal val)
        return (decimal)Math.Ceiling((double)val - 0.5);
        return (decimal)Math.Floor((double)val + 0.5);


"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
Old 07-09-2004, 11:06 AM
DeFlipMode DeFlipMode is offline
Join Date: Jul 2004
Posts: 3
DeFlipMode is on a distinguished road

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.

Reply With Quote


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 08: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 03:44 PM


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.
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
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..