hcougar
asked on
Format number field to zero's
I am currently printing out a field that is 12 characters long. How do I need to write the code so that there are leading zeros in the field. Basically, what is the code to make it look like the following:
1234567 ==> 000001234567
Always starts off with zeros on the left side and amount on left could vary but always 12 characters, could also be:
123456789 ==> 000123456789
1234567 ==> 000001234567
Always starts off with zeros on the left side and amount on left could vary but always 12 characters, could also be:
123456789 ==> 000123456789
newstring=Format("1234567" , "000000000000")
hcougar,
Use the FORMAT function, as shown below:
Dim x As Double
x = 12345678
Text1.Text = Format(x, "000000000000")
Ture Magnusson
Karlstad, Sweden
Use the FORMAT function, as shown below:
Dim x As Double
x = 12345678
Text1.Text = Format(x, "000000000000")
Ture Magnusson
Karlstad, Sweden
ASKER
Crazyman, the field is going to vary. It won't always be "1234567". I am reading data from a txt file.
ASKER
ture, that is always going to give me 12345678. The field will vary.
So in real terms you could have a small function.
Public function ChangeString(OldString as string,NumOfZeros as integer)as string
ChangeString=format(oldStr ing,string (numofzero s,chr(48)) )
end function
then use it like
Private Sub Command1_Click()
MsgBox ChangeString("1234567", 12)
End Sub
Public function ChangeString(OldString as string,NumOfZeros as integer)as string
ChangeString=format(oldStr
end function
then use it like
Private Sub Command1_Click()
MsgBox ChangeString("1234567", 12)
End Sub
it will work on any number string not just 1234567 ..just replace it.
1234567 was meant as an example, it will work on any string of numbers you just need to pass what ever your numbers are in place of 1234567
ASKER
Maybe I'm not making this clear. Here is the data I want to print out.
000000123456
000004521458
000215465542
000000001248
000045236554
000004542125
000000123456
000004521458
000215465542
000000001248
000045236554
000004542125
hcougar,
The answer both ture and crazyman have gave you, is the right answer.
You should use the Format function for what you want
<textout>=Format(<Value>," 0000000000 00")
Gordon
The answer both ture and crazyman have gave you, is the right answer.
You should use the Format function for what you want
<textout>=Format(<Value>,"
Gordon
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Function AlterString(sText As String) As String
Dim strText As String
Dim lngLen As Long
strText = sText
lngLen = Len(strText)
AlterString = String(12 - lngLen, "0") & strText
End Function
call the function as follows
text1.text=alterstring(tex t1.text)
Dim strText As String
Dim lngLen As Long
strText = sText
lngLen = Len(strText)
AlterString = String(12 - lngLen, "0") & strText
End Function
call the function as follows
text1.text=alterstring(tex