Discussion:
formatting text to include leading 0's
(too old to reply)
m***@gmail.com
2013-08-27 17:32:21 UTC
Permalink
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 )
Dave "Crash" Dummy
2013-08-27 18:38:08 UTC
Permalink
Post by m***@gmail.com
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
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)
--
Crash

"I am not young enough to know everything."
~ Oscar Wilde ~
Evertjan.
2013-08-27 19:24:33 UTC
Permalink
Dave "Crash" Dummy wrote on 27 aug 2013 in
Post by Dave "Crash" Dummy
Post by m***@gmail.com
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
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.
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
2013-08-27 20:23:29 UTC
Permalink
| > 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.
2013-08-27 20:46:35 UTC
Permalink
Post by Mayayana
| > 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.
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?
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
b***@gmail.com
2014-12-21 00:45:01 UTC
Permalink
Post by Dave "Crash" Dummy
I'm sure Les appreciates that, if he is still alive. After all, it was
over 12 years ago that he asked.
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
2014-12-21 03:03:38 UTC
Permalink
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
2013-08-28 08:47:19 UTC
Permalink
Thats over 12 *years* ago ... :-)
Todd Vargo
2013-08-28 09:50:41 UTC
Permalink
Post by R.Wieser
Thats over 12 *years* ago ... :-)
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
2013-08-28 15:59:07 UTC
Permalink
Post by Todd Vargo
Yes, Crash's opening comment noted it.
Shucks, that I missed that ... :-\
b***@gmail.com
2014-12-21 00:40:38 UTC
Permalink
Post by m***@gmail.com
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
your_number = 13
msgbox right( your_number + 10000, 4 )
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.
R.Wieser
2014-12-21 09:27:41 UTC
Permalink
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).
Post by b***@gmail.com
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
Post by b***@gmail.com
Post by m***@gmail.com
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
your_number = 13
msgbox right( your_number + 10000, 4 )
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.
k***@gmail.com
2015-07-10 06:52:00 UTC
Permalink
This was very helpfull. Thank you:-)
Evertjan.
2015-07-10 07:17:19 UTC
Permalink
Post by k***@gmail.com
This was very helpfull. Thank you:-)
What was?

Hey, this is not email!
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
p***@gmail.com
2016-03-09 12:37:29 UTC
Permalink
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

Loading...