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

leading zeros

Status
Not open for further replies.

mako

Programmer
Apr 6, 2000
11
MY
how do i format a number to display 5 digits with leading zeros? e.g. integer '1' will be displayed as '00001' and integer 12 will be displayed as '00012'.

- mako - [sig][/sig]
 
here is a little function that i use:

Function AddLeadingZeros(iLen, sStr)
sStr = CStr(sStr)
Do While Not Len(sStr) = iLen
sStr = "0" & sStr
Loop
AddLeadingZeros = sStr
End Function

you would call it like:

Dim sFormattedNumber
sFormattedNumber = AddLeadingZeros(5, CStr(numbertoformat))

note that the numbers become strings, and are no longer integers
[sig]<p>ray<br><a href=mailto:rheindl@bju.edu>rheindl@bju.edu</a><br><a href= > </a><br> [/sig]
 
mako,

Frogger/ray's function is somewhat cumbersome for a simple display,

[tab]MyVal = 315
[tab]NumPad = 5

[tab]? Format(MyVal, String(NumPad, &quot;0&quot;))
[tab]00315

is just done in the &quot;immediate&quot; window. Of course, you can ignore the NumPad thing and just put the &quot;5&quot; directly into the format statement and get the same results.

[sig]<p>MichaelRed<br><a href=mailto:mred@duvallgroup.com>mred@duvallgroup.com</a><br>There is never time to do it right but there is always time to do it over[/sig]
 
hmmmm, Sorry about that. Seems like a useful and friendly way to help w/ display values. Anyway, still no need to jump through loop(s),

MyVal = 315
? Right(String(5, &quot;0&quot;) & Trim(Str(MyVal)), 5)
00315

unless what ever language mako is using (he will let us know - won't he?) doesn't support some part of this. [sig]<p>MichaelRed<br><a href=mailto:mred@duvallgroup.com>mred@duvallgroup.com</a><br>There is never time to do it right but there is always time to do it over[/sig]
 
thanks for pointing out guys. i've tried both of your methods Michael. works like a charm.

- mako - [sig][/sig]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top