Visual Basic E (E for Express) provides an affordable way for software developers to use the Microsoft .NET framework. Express editions of Visual Basic are freely available on the Microsoft official website. Decimal numbers are common data types used when programming in the Visual Basic language. By default, these numbers span several places after the decimal point. However, you can format the output and set the amount of decimal places shown in the application.
Create a decimal variable. The decimal data type is defined using the "decimal" keyword. The following code defines a decimal data type variable:
Dim dNumber As Decimal
Assign a value to the variable. In this example, you are defining a decimal with five numbers after the decimal point. The following code shows you how to assign values to VB variables:
dNumber = 3.14798
Round to the nearest decimal place and format the number. In this example, the number is formatted to two decimal places. Because the third decimal place is "7," the number is rounded to 3.15. The following formats your decimal number:
The code above converts the number to a string and sets the decimal place using the "N2" text. The number in this text setting indicates how many decimal places are used. For example, "N3" formats it with 3 decimal places, "N4" uses 4 decimal places.
Print the result to the screen to test your code. To verify the decimal format completed successfully, use the following command to show the results:
- Photo Credit The mechanical counter. A close up on a blue background. image by Andrey Khritin from Fotolia.com
How to Write Calculation Formulas in Visual Basic
Calculation formulas written in Visual Basic use numbers, symbols and various operators to produce mathematical values. Making calculations for addition, subtraction, multiplication...
How to 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...
How to Read Decimal Place Value
Understanding how to read a number with decimals is the first step in understanding what the decimal is representing. There is a...
How to Convert a Textbox to a Number in Visual Basic
Microsoft's Visual Basic contains some powerful functions that make the programmer's life easier. One such example is the ability to convert data...
Different Data Types in Visual Basic
Computer languages like Visual Basic identify and categorize data based on its type; for instance, character-based data is categorized differently from numeric...