Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleRound numbers to a given number of digits without using banker's rounding (version 2) in Visual Basic 2005
DescriptionThis example shows how to round numbers to a given number of digits without using banker's rounding (version 2) in Visual Basic 2005.
Keywordsround, banker's rounding, digits, Math.Round, VB 2005
CategoriesMiscellany, Tips and Tricks
 
Normally Visual Basic uses "banker's rounding." In banker's rounding, numbers with final digit 5 are rounded to the nearest even number, not to the next larger number. The idea is that statistically half of a sample of numbers are rounded up and half are rounded down.

The following code rounds the variable num to four digits and rounds away from zero (up for positive numbers, down for negative numbers) if a number's final digit is 5.

 
Math.Round(num, 4, MidpointRounding.AwayFromZero)
 
When you change the number in the program's TextBox, the code displays the number rounded to 1, 2, 3, and 4 digits past the decimal point using banker's rounding and non-banker's rounding so you can see the difference.
 
 
Copyright © 1997-2006 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated