Wednesday, July 3, 2013

Round A Number In Visual Basic

Round a Number in Visual Basic


The basic number data types in Visual Basic all come with helper functions for rounding numbers, but the precise function used depends on the rules you wish to use to round the number. For example, numbers are rounded down using the "floor" function, rounded up using the "ceiling" function, and rounded to the nearest whole number (with 0.5 rounded up) using the "round" function.








Instructions


1. Create a new project by clicking "File" and "New Project." Select "Console Application" as the project type. Later, you can duplicate the same code in a graphical user interface (GUI) based application. Name the project "NumberRoundingTutorial."


2. Create a number to round by pasting the following code into your project:


Dim mynum as Double


mynum = 0.5


3. Paste the following code to round the number up to the next highest whole number:


dim roundup as integer = math.ceiling(mynum)


4. Paste the following to round the number down to the next lowest whole number:


dim rounddown as integer = math.floor(mynum)


5. Paste the following to round the number to the nearest whole number, up or down:


dim round as integer = math.round(mynum, MidPointRounding.AwayFromZero)

Tags: whole number, integer math, mynum Paste, mynum Paste following, Paste following, round number