Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Integers

Status
Not open for further replies.

fergmj

Programmer
Feb 21, 2001
276
US
I have a number 0001 and I want the 000 to stay with the nuber. VB keeps making it just 1.

How can I force the 000?

thanks

fergmj
 
Try this function:

Option Explicit

Private Function AddZeros(ByVal Num As Variant, NumOfZeros_ As Integer) As Variant

Dim a As Integer

For a = NumOfZeros To 1 Step -1
Num = "0" & Num
Next a

AddZeros = Num

End Function

'USAGE:

Private Sub Command1_Click()
MsgBox AddZeros(1, 3)

End Sub

 
Use the Format$ function as shown below:

Dim intInput As Integer
Dim strOutput As String

intInput = 1
strOutput = Format$(intInput, "0000")
Debug.Print strOutput

intInput = 123
strOutput = Format$(intInput, "0000")
Debug.Print strOutput

If the number has fewer digits than there are zeroes in the Format expression, leading zeroes will be displayed. The number of zeroes will be automatically adapted to the magnitude of the number.

As integers have at most 5 digits, you should use Format$(intYourInteger, "00000") to be on the safe side.
_________________________________
In theory, there is no difference between theory and practice. In practice, there is. [attributed to Yogi Berra]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top