Format numbers in VB

This is a FAQ for VB Beginners that deals with the problem of numbers that have more digits after the decimal point than you want to use or display.

Topics covered are:


a. Use the "Fixed" Format and Format$
b. Use the "Standard" Format and Format$
c. Use Format$ and your own settings
d. Use FormatNumber
e. Use the Round Function

Sometimes calculations in your project will result in numbers with a lot of decimal places - users input numbers with multiple digits after the decimal point or the calculation throws up a long list of decimal digits that you don't want or need you want to restrict these numbers to, say, 2 decimal places, here are a few ways you can do this:


a. Use the "Fixed" Format and Format$

This example takes a user input from a textbox and performs a calculation on it. The result, correct to 2 decimal places, is shown in a label. If your project needs are different, you can adapt the code as necessary.

Add a textbox (Text1), a commandbutton(Command1) and a Label (Label1) to a form, then copy and paste this code:-

Code:Option Explicit
Dim lngDemoNo As Double

Private Sub Form_Load()
Text1 = 123764.76464646 ' You can change this at run time if you want
lngDemoNo = Text1 * 3.142 ' Create some demo data
End Sub

Private Sub Command1_Click()
' For demo purposes, display the result
Label1 = "Your Input Number * Pi = " & Format$(lngDemoNo, "Fixed")
End Sub


b. Use the "Standard" Format and Format$

The only difference to the above is that this will insert commas if the value of the number is greater than 999 (ie. 1,000.00 instead of 1000.00).

You can recyle the code example above and just change the code line in the Command1_Click event to:

Code:Label1 = "Your Input Number * Pi = " & Format$(lngDemoNo, "Standard")


c. Use Format$ and your own settings

You can force the result to appear in whatever format you like by using Format$ and creating the settings yourself. Re-use the first example and change the code line in the Command1_Click Event to:

Code:Label1 = "Your Input Number * Pi = " & Format$(lngDemoNo, "0.000")

Notice that I have put three zeros after the decimal point, so the resulting label display is shown correct to three decimal places. You can, of course, change this to whatever suits your purposes.


d. Use FormatNumber

You can also use the FormatNumber Function provided by VB. This function will accept several arguments that can be used to present a number in different ways, but we will just use one in this FAQ.

Once again, replace the Command Button code line with this one:

Code:Label1 = "Your Input Number * Pi = " & FormatNumber(lngDemoNo, 3)

The "3" digit is the number of decimal places to use. Again, you can change this to whatever you want.


e. Use the Round Function

Similar to most of the above options, the Round Function will return a value set to the number of places you decide and will round the last digit up or down.

The replacement code line in this case is:

Code:Label1 = "Your Input Number * Pi = " & Round(lngDemoNo, 2)

So there you have various ways of achieving the same thing – dictating how many digits you want to have after the decimal point.

0 comments: