Home All Groups Group Topic Archive Search About

Format a Numeric to include leading zeros

Author
22 Oct 2005 3:51 AM
Saucer Man
How do I format a numeric to include leading zeros?  I want to do a loop
that will name files 099 down to 001.  The zeros are getting dropped and I
don't want them to.  Thanks.

Author
22 Oct 2005 3:58 AM
mscir
Saucer Man wrote:
> How do I format a numeric to include leading zeros?  I want to do a loop
> that will name files 099 down to 001.  The zeros are getting dropped and I
> don't want them to.  Thanks.

     Dim n As Long
     For n = 99 To 1 Step -1
         Debug.Print Format(n, "000")
     Next n

Mike
Author
22 Oct 2005 4:03 AM
Ralph
"Saucer Man" <saucerman@nospam.net> wrote in message
news:NcedneTrJ_nWKsTeRVn-sw@adelphia.com...
> How do I format a numeric to include leading zeros?  I want to do a loop
> that will name files 099 down to 001.  The zeros are getting dropped and I
> don't want them to.  Thanks.
>

   Dim sTmp = Format( nCnt, "000")
Author
22 Oct 2005 4:12 AM
Rick Rothstein [MVP - Visual Basic]
>    Dim sTmp = Format( nCnt, "000")

Not in VB6 (you can't assign values in a Dim statement).

Rick
Author
22 Oct 2005 4:26 AM
Ralph
"Rick Rothstein [MVP - Visual Basic]" <rickNOSPAMnews@NOSPAMcomcast.net>
wrote in message news:uDgLL7r1FHA.3560@TK2MSFTNGP15.phx.gbl...
> >    Dim sTmp = Format( nCnt, "000")
>
> Not in VB6 (you can't assign values in a Dim statement).
>
> Rick
>

<That's the trouble with switching back and forth all day long.>

      Dim sTmp As String : sTmp = Format(nCnt, "000")
Author
23 Oct 2005 2:28 PM
Saucer Man
Thanks all!  I didn't realize the "000" would work for numerics.

--

Thanks.


Show quoteHide quote
"Saucer Man" <saucerman@nospam.net> wrote in message
news:NcedneTrJ_nWKsTeRVn-sw@adelphia.com...
> How do I format a numeric to include leading zeros?  I want to do a loop
> that will name files 099 down to 001.  The zeros are getting dropped and I
> don't want them to.  Thanks.
>
>
>
>
Author
23 Oct 2005 2:36 PM
Duane Bozarth
Saucer Man wrote:
>
> Thanks all!  I didn't realize the "000" would work for numerics.
>
.....

What would it work for then?  (Curious about the comment as the only
place "0" appears in the descriptions of formatting is in user-defined
numeric formats.)
Author
24 Oct 2005 1:05 AM
Ralph
"Duane Bozarth" <dpboza***@swko.dot.net> wrote in message
news:435B9FD1.A8FF7C59@swko.dot.net...
> Saucer Man wrote:
> >
> > Thanks all!  I didn't realize the "000" would work for numerics.
> >
> ....
>
> What would it work for then?  (Curious about the comment as the only
> place "0" appears in the descriptions of formatting is in user-defined
> numeric formats.)

Some examples show it as ...
    Dim sTmp As String
    sTmp = Format("123", "0000")

I've run into quite a few new students, who associate "Format" with String
and Data formatting only and assume the seed string can only be a String or
Date.
Not trying to speak for the OP, but it does happen.

-ralph
Author
24 Oct 2005 1:24 AM
Duane Bozarth
Ralph wrote:
Show quoteHide quote
>
> "Duane Bozarth" <dpboza***@swko.dot.net> wrote in message
> news:435B9FD1.A8FF7C59@swko.dot.net...
> > Saucer Man wrote:
> > >
> > > Thanks all!  I didn't realize the "000" would work for numerics.
> > >
> > ....
> >
> > What would it work for then?  (Curious about the comment as the only
> > place "0" appears in the descriptions of formatting is in user-defined
> > numeric formats.)
>
> Some examples show it as ...
>     Dim sTmp As String
>     sTmp = Format("123", "0000")
>
> I've run into quite a few new students, who associate "Format" with String
> and Data formatting only and assume the seed string can only be a String or
> Date.
> Not trying to speak for the OP, but it does happen.

But that's still applying the "0000" format field to a numeric field.
Observe the results of

?Format("abc", "0000")
abc

which demonstrates that again it's the silent VB type coercion in play
here.
Author
24 Oct 2005 1:56 AM
Ralph
Show quote Hide quote
"Duane Bozarth" <dpboza***@swko.dot.net> wrote in message
news:435C37BD.775CC3C7@swko.dot.net...
> Ralph wrote:
> >
> > "Duane Bozarth" <dpboza***@swko.dot.net> wrote in message
> > news:435B9FD1.A8FF7C59@swko.dot.net...
> > > Saucer Man wrote:
> > > >
> > > > Thanks all!  I didn't realize the "000" would work for numerics.
> > > >
> > > ....
> > >
> > > What would it work for then?  (Curious about the comment as the only
> > > place "0" appears in the descriptions of formatting is in user-defined
> > > numeric formats.)
> >
> > Some examples show it as ...
> >     Dim sTmp As String
> >     sTmp = Format("123", "0000")
> >
> > I've run into quite a few new students, who associate "Format" with
String
> > and Data formatting only and assume the seed string can only be a String
or
> > Date.
> > Not trying to speak for the OP, but it does happen.
>
> But that's still applying the "0000" format field to a numeric field.
> Observe the results of
>
> ?Format("abc", "0000")
> abc
>
> which demonstrates that again it's the silent VB type coercion in play
> here.
>


Which would have been useful additional information for Saucer Man. Now
isn't it fun to share?

-ralph
Author
24 Oct 2005 1:51 PM
Duane Bozarth
Ralph wrote:
Show quoteHide quote
>
> "Duane Bozarth" <dpboza***@swko.dot.net> wrote in message
> news:435C37BD.775CC3C7@swko.dot.net...
> > Ralph wrote:
> > >
> > > "Duane Bozarth" <dpboza***@swko.dot.net> wrote in message
> > > news:435B9FD1.A8FF7C59@swko.dot.net...
> > > > Saucer Man wrote:
> > > > >
> > > > > Thanks all!  I didn't realize the "000" would work for numerics.
> > > > >
> > > > ....
> > > >
> > > > What would it work for then?  (Curious about the comment as the only
> > > > place "0" appears in the descriptions of formatting is in user-defined
> > > > numeric formats.)
> > >
> > > Some examples show it as ...
> > >     Dim sTmp As String
> > >     sTmp = Format("123", "0000")
> > >
> > > I've run into quite a few new students, who associate "Format" with
> String
> > > and Data formatting only and assume the seed string can only be a String
> or
> > > Date.
> > > Not trying to speak for the OP, but it does happen.
> >
> > But that's still applying the "0000" format field to a numeric field.
> > Observe the results of
> >
> > ?Format("abc", "0000")
> > abc
> >
> > which demonstrates that again it's the silent VB type coercion in play
> > here.
> >
>
> Which would have been useful additional information for Saucer Man. Now
> isn't it fun to share?

I guess, and if I knew that were the question I'm sure I would
have...but I didn't (and still don't know what he meant...) :(