How to Input a Number to Two Decimal Places in Visual Basic
- 1). Create a variable and assign it a decimal number. Although you can use a number directly, the best standard in programming is to assign numbers to a variable first. Below is an example of a defined variable that will be formatted in the code:
Dim myDecimal as Decimal
myDecimal = 1.90
In this example, Visual Basic will automatically truncate the last zero, which is why you use formatting techniques for applications for information like billing and financial data. - 2). Assign a new value by using the round function. The round function defines the number of decimal places by using the second parameter. Below is an example of the round function:
myDecimal = Math.Round (1.99999, 2)
This result rounds to 2.00. - 3). Convert to text and assign to a text box. You may also want to assign the number to a text box, so it needs to be converted to a string first. The following example converts the decimal number from Step 1 and assigns it to the text box, formatting the two numbers after the decimal place.
myTextbox.Text = myDecimal.ToString("#,##0.00")