Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

formatting text to include leading 0's

4,416 views
Skip to first unread message

Les F.

unread,
Jun 5, 2001, 11:23:07 PM6/5/01
to
Hello:
Is there a way to format a text string similiar to VB's
format$ function?. I would like to include leading zero's.
I have looked at formatnumber and formatcurrency but I
don't think they let me decide how many leading zeros to
use.

Les F


Randy Hunt, MCSE

unread,
Jun 6, 2001, 2:35:16 AM6/6/01
to
Les F. says:


It's not hard to make your own. Or you can use mine:
http://www.serverscripting.com/info.asp?id=1027


----------
Randy Hunt
MCP+I / MCSE

Please visit my web sites for free scripts!
http://www.serverscripting.com
http://www.wshscripting.com

Respond_Within@newsgroup.com Walter Zackery

unread,
Jun 6, 2001, 2:50:17 AM6/6/01
to
Try the function below, where the parameter TheNumber can be either a
string or number, and the parameter Amount is the number of leading
zeroes to add.

Function AddLeadingZeroes(TheNumber,Amount)
AddLeadingZeroes = String(Amount,"0") & TheNumber
End Function

"Les F." <za...@hotmail.com> wrote in message
news:e5bf1bj7AHA.1340@tkmsftngp04...

Harvey Colwell

unread,
Jun 6, 2001, 10:25:42 AM6/6/01
to
Here is a pair of functions I use. The first is written to pad a numeric
"Value" with zeros so that it is 4 characters long. The second one actually
does the work. It is generic and can pad any value (numeric or string) using
any character (not just zero) to any length.


'---------------------
Function AddLeadingZeros(Value)
If Len(Value) < 4 And IsNumeric(Value) then
Value = Pad(Value, 4, 0)
End If
AddLeadingZeros = Value
End Function

'---------------------
Function Pad(Value, Width, Char)
Pad = Right(String(Width, CStr(Char)) & CStr(Value), Width)
End Function

"Les F." <za...@hotmail.com> wrote in message
news:e5bf1bj7AHA.1340@tkmsftngp04...

miket...@gmail.com

unread,
Aug 27, 2013, 1:32:21 PM8/27/13
to
Just do that:

your_number = 13

msgbox right( your_number + 10000, 4 )



Dave "Crash" Dummy

unread,
Aug 27, 2013, 2:38:08 PM8/27/13
to
I'm sure Les appreciates that, if he is still alive. After all, it was
over 12 years ago that he asked.

I do it this way, but I don't guess it matters:

msgbox right("0000" & your_number,4)
--
Crash

"I am not young enough to know everything."
~ Oscar Wilde ~

Evertjan.

unread,
Aug 27, 2013, 3:24:33 PM8/27/13
to
Dave "Crash" Dummy wrote on 27 aug 2013 in
microsoft.public.scripting.vbscript:

> miket...@gmail.com wrote:
>> On Tuesday, June 5, 2001 11:16:29 PM UTC-4, Les F. wrote:
>>> Hello: Is there a way to format a text string similiar to VB's
>>> format$ function?. I would like to include leading zero's. I have
>>> looked at formatnumber and formatcurrency but I don't think they
>>> let me decide how many leading zeros to use.
>>>
>>> Les F
>>
>> Just do that:
>>
>> your_number = 13
>>
>> msgbox right( your_number + 10000, 4 )
>
> I'm sure Les appreciates that, if he is still alive. After all, it was
> over 12 years ago that he asked.
>
> I do it this way, but I don't guess it matters:
>
> msgbox right("0000" & your_number,4)

msgbox right("000" & your_number,4)

I guess it doesn't matter more than one character.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

Mayayana

unread,
Aug 27, 2013, 4:23:29 PM8/27/13
to
| > msgbox right("0000" & your_number,4)
|
| msgbox right("000" & your_number,4)
|
| I guess it doesn't matter more than one character.
|
And he also won't need to test to make sure
he's not starting with an empty string.

Actually it won't cause an error to do
MsgBox("000", 4), but neither will it yield a
4-character string.


Evertjan.

unread,
Aug 27, 2013, 4:46:35 PM8/27/13
to
One should make sure your_number is a valid number,
integer, zero or positive, less than or equal to 9999.

Try wrong results for:

your_number = 3.9
your_number = "4.0"
your_number = "loss_of_memory"
your_number = -7
your_number = 10012

So why make a special case for an empty string?

R.Wieser

unread,
Aug 28, 2013, 4:47:19 AM8/28/13
to
Guys, I guess you missed the origional date :

> On Tuesday, June 5, 2001 11:16:29 PM UTC-4, Les F. wrote:

Thats over 12 *years* ago ... :-)



Todd Vargo

unread,
Aug 28, 2013, 5:50:41 AM8/28/13
to
Yes, Crash's opening comment noted it.
1-Upsmanship never sleeps though. :)

--
Todd Vargo
(Post questions to group only. Remove "z" to email personal messages)

R.Wieser

unread,
Aug 28, 2013, 11:59:07 AM8/28/13
to
> Yes, Crash's opening comment noted it.

Shucks, that I missed that ... :-\



becc...@gmail.com

unread,
Dec 20, 2014, 7:40:41 PM12/20/14
to
Thanks for posting this---it helped me very quickly come up with a solution for my own "leading zeros" dilemma in a very simple line of code.

becc...@gmail.com

unread,
Dec 20, 2014, 7:45:03 PM12/20/14
to

> I'm sure Les appreciates that, if he is still alive. After all, it was
> over 12 years ago that he asked.
>
> I do it this way, but I don't guess it matters:
>
> msgbox right("0000" & your_number,4)
> --
> Crash
>
> "I am not young enough to know everything."
> ~ Oscar Wilde ~

Oh, actually, THIS is the method that I used to solve my issue in a single line of code. Thanks for posting this, and who cares if it was long after the original post. Some of us trolling for answers today really appreciate seeing alternate solutions for our coding problems.

Mayayana

unread,
Dec 20, 2014, 10:03:56 PM12/20/14
to

Oh, actually, THIS is the method that I used to solve my issue in a single
line of code. Thanks for posting this, and who cares if it was long after
the original post. Some of us trolling for answers today really appreciate
seeing alternate solutions for our coding problems.
>

And now that you've figured that out you might want
to look into getting a real newsreader and getting off
of Google Groups, so that you can respond to posts
that were posted this year and this decade, so that
someone might actually know and care what you're
talking about.


R.Wieser

unread,
Dec 21, 2014, 4:24:56 AM12/21/14
to
beccanet,

Try: your_number = 13.123 . I don''t think your "leading zero" solution
will return anything you like. Same for a value like 65535 or something
like -42. :-)

That means you either did not give us all the info needed (like you only use
whole, positive values in a certain range), or you didn't think it fully
thru ...

In other words: "simple solutions" are often only that because they ignore a
lot of possible cases. Be aware of it (and preferrably mention it when
offering the solution to others).

> Thanks for posting this, and who cares if it was long after the original
post.

As I do not see that post here I'm assuming he emailed it to you personally,
and you made a mistake by replying to it in the group.

And some people here do really care. Like me. In your case its not really
a problem (you only expressed your gratitude to a soluton), but some posters
seem to think that answering a question that was asked years ago is good.
Even worse, people who seem to think that they can just continue a years old
conversation (as if the involved people are still around and know what it
was about).

Regards,
Rudy Wieser


-- Origional message:
<becc...@gmail.com> schreef in berichtnieuws
8499e4d3-769d-4780...@googlegroups.com...

karen...@gmail.com

unread,
Jul 10, 2015, 2:52:09 AM7/10/15
to
This was very helpfull. Thank you:-)

Evertjan.

unread,
Jul 10, 2015, 3:17:18 AM7/10/15
to
karen...@gmail.com wrote on 10 Jul 2015 in
microsoft.public.scripting.vbscript:

> This was very helpfull. Thank you:-)

What was?

Hey, this is not email!

ple...@gmail.com

unread,
Mar 9, 2016, 7:37:35 AM3/9/16
to
i did it this way, but dont know if that is your requirement:

Function AddLeading(Value As String, Leading As Integer, LeadingChar As String) As String

If Len(Value) < Leading Then

Dim intDiff As Integer
intDiff = Leading - Len(Value)
Dim strLeadingZeroes As String
strLeadingZeroes = ""

Dim i As Integer
For i = 1 To intDiff Step 1
strLeadingZeroes = strLeadingZeroes & "0"
Next i

Value = strLeadingZeroes & Value

End If

AddLeading = Value
End Function
0 new messages