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 Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Formatting Text

Status
Not open for further replies.

SmokinWrek

IS-IT--Management
Jun 20, 2002
36
US
OK, I've got a program in which I am writing out fixed-length records to a file opened for Output. Some of the fields in the record are numbers calculated within the program. I need to output these numbers into a fixed length field and have leading zeroes.

Example: calculated value is 4000 but I want that in a 5 byte field with leading zeroes so it looks like 04000 in the field.


How can I accomplish this? I can get it to be in a fixed length field by defining the length of the string variable used for that field, but then I get the number left justified without the leading zero, i.e. - 4000 <---note trailing space after last zero.

I've tried using the Format function, but either I don't know enough about it or it can't do what I want.

Any ideas?
 
Is this what you're looking for:
Code:
Private Function FormatNumTo5Chars(strTest As String) As String
  FormatNumTo5Chars = String$(5 - Len(strTest), "0") & strTest
End Function
 
Oops...you'd have to had for if it's longer than 5 digits:
Code:
Private Function FormatNumTo5Chars(strTest As String) As String
  [b]If Len(strTest) > 5 Then
    FormatNumTo5Chars = strTest
  Else[/b]
    FormatNumTo5Chars = String$(5 - Len(strTest), "0") & strTest
  [b]End If[/b]
End Function
 
Aghhh..."handle" not "had".
...(let's see how many times I can reply to my own posts)...
 
SleepDepD,

Thanks for the responses, but I was hoping to avoid having to do something like that for every field that needs it.

Fortunately I found out a bit more about the Format function, and I got it to do what I need (I was just missing some quote marks around my format).

Here's what I'm using:

Format(Boxsize,"00000")


Thanks for the quick reply.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top