Marine Corps Leadership Secrets - Part IIIMarine Corps Leadership Secrets - Part IV

What You Should Know About Rounding in VB.NET

July 31st, 2007

RoundingRounding, the process of reducing the number of significant digits in a number, should be a simple enough but there are actually several ways of doing this that we need to be concerned about. In this article, I’ll go over the different types of rounding found in VB.NET.

Simple Rounding

This form of rounding is simple truncation of the number where any digits after the point of precision are dropped. VB.NET provides two functions for doing this to get an integer result, Int and Fix. However these two functions are slightly different. Both remove the fractional part of a number but, when a number is negative, Int returns the nearest negative integer less than or equal to the number while Fix does the opposite. For example, Fix(-10.4) returns a -10 while Int(-10.4) returns -11.

The way the Fix function works is called symmetric rounding since it affects positive and negative numbers the same way. The Int function is considered asymmetric rounding because it affects positive and negative numbers differently.

You can also do truncation rounding using the Format function, like so:

Format(48.15162342, "#.00")

This is method doesn’t take the trailing numbers into account at all so care should be exercised when using it to avoid skewing results.

It is better to use Math functions for rounding rather than using these methods, particularly if statistical precision is required.

Arithmetic Rounding

When you use simple rounding, always going up or down, you run into the problem about what to do with numbers that are right at the midpoint. What should 1.5 be rounded to? 1 or 2?

By mathematical convention, numbers at the midpoint are rounded up symmetrically although you will see examples of asymmetric rounding in some math libraries, most notably some Java/Javascript implementations.

In VB.NET we can do this kind of rounding using the Math Namespace, like so:

Math.Round(48.15162342, 2, MidpointRounding.AwayFromZero)

Note the last parameter. This tells the function to perform arithmetic symmetric rounding instead of the default bankers rounding.

Bankers Rounding

This is the kind of rounding used by default in the .NET Math library and by built-in VB.NET functions such as CInt(). In this type of rounding, .5 rounds to the nearest even number. This means that both 1.5 and 2.5 round to 2, and 3.5 and 4.5 both round to 4. The idea behind this method is that it prevents a round up or down bias from entering into a large series of numbers.

This method has caused a lot of confusion over the years because in previous versions of VB this method of rounding was the only one available. Since some project sponsors didn’t like it and some did it often made rounding in a program a pain. Fortunately with the .NET Framework 2.0 we can implement rounding either way in the Math.Round function so that we can please everybody.

I would strongly recommend that you have rounding methods spelled out in your requirements. If you don’t, expect headaches down the road. Also, you should use Math.Round instead of built-in functions that perform rounding so that if there is a last minute change in rounding technique, it’s easier to change.

Stochastic or Statistical Rounding

One last method of rounding we should look at is Stochastic or statistical rounding. In this method, when a number to be rounded falls at the midpoint, the decision to round up or down is determined randomly. In theory, this method prevents large totals from becoming skewed. However, using this method will make statistical analysis hard to replicate and requires more computational time. You can implement this using the .NET Random functions. This method is good for certain types of statistics but is probably overkill for most rounding implementations.

Hopefully this article has helped you gain a better understanding of rounding in VB.NET. If you have any questions or ideas about rounding, please feel free to leave me a comment.

Share This Article: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Reddit
  • StumbleUpon
  • Technorati
  • DotNetKicks
  • DZone

Entry Filed under: Tip Sheets


Rate This Article:

Not That GoodCould Be BetterOKGoodGreat (11 votes, average: 4.55 out of 5)
Loading ... Loading ...

5 Comments Add your own

  • 1. Stephen Slocum  |  September 11th, 2008 at 9:20 am

    I’m having a hard time with Math.Round. Any idea why:

    Math.Round(515.435, 2, MidpointRounding.AwayFromZero)

    returns 515.43 in VB.NET 2008? I really, really need it to return 515.44.

  • 2. deepak v panchal  |  November 24th, 2008 at 11:14 pm

    use ToString Method as follow

    double valuetoround = 515.435;

    roundvalue = valuetoround.ToString(”#.00″);

    You will get your required ans. 515.44 instead of 515.43

  • 3. dennis  |  June 26th, 2009 at 1:55 am

    double valuetoround = 515.435;

    roundvalue = valuetoround.ToString(”#.00″);

    this method seem to be flawed also just try :

    515.4445 it will round to 515.44 instead of 515.45

  • 4. marc  |  September 18th, 2009 at 7:03 am

    dennis, #.00 is correct.
    515.4445 when rounded to 2 decimal places is 515.44.
    This is because the number in the third decimal place (4) is less than 5.
    515.4454 would round to 515.45, because in this case the 3rd decimal place (5) >= 5

  • 5. mahan  |  October 12th, 2009 at 8:04 pm

    you can use a variable declared as Decimal and assigned value is 0 then add it to your variable you want to set precise

Leave a Comment

Required

Required, hidden

Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Trackback this post  |  Subscribe to the comments via RSS Feed


Visit Me At My New Site, Programming In C#

Most Popular Articles

Highest Rated Articles

Categories

Most Recent Articles

Feeds

 Subscribe in a reader

To subscribe by e-mail
Enter your address here

Delivered by FeedBurner

VB Opportunities

Archives