How to Fix Number of Decimal Places in Visual Basic E

Save

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:

    dNumber.ToString("N2")

    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:

    MsgBox dNumber.ToString("N2")

References

  • Photo Credit The mechanical counter. A close up on a blue background. image by Andrey Khritin from Fotolia.com
Promoted By Zergnet

Comments

You May Also Like

Related Searches

Check It Out

Geek Vs Geek: Robot battles, hoverboard drag race, and more

M
Is DIY in your DNA? Become part of our maker community.
Submit Your Work!