Home All Groups Group Topic Archive Search About

How can I trim off leading zeros from a number? Help greatly appreciated!!!

Author
11 May 2007 6:26 PM
jonny
I am trying to trim (remove) off leading zero's from a number in a
text box. The number may be anything from 0001 to 0999.

Is this possible? Your help is greatly appreciated.

Author
11 May 2007 6:33 PM
Grand_Poobah
strText = Trim(Str(Val(txtBoxName.Text)))

GP

--->
Show quote
> I am trying to trim (remove) off leading zero's from a number in a
> text box. The number may be anything from 0001 to 0999.
>
> Is this possible? Your help is greatly appreciated.
>
Author
11 May 2007 6:40 PM
jonny
On May 11, 1:33 pm, Grand_Poobah
<iss_boss.del***@delete.sbcglobal.net> wrote:
Show quote
> strText = Trim(Str(Val(txtBoxName.Text)))
>
> GP
>
> --->
>
>
>
> > I am trying to trim (remove) off leading zero's from a number in a
> > text box. The number may be anything from 0001 to 0999.
>
> > Is this possible? Your help is greatly appreciated.- Hide quoted text -
>
> - Show quoted text -

Thanks Grand. Where do I tell it to remove zeros.


Dim strtext As String
strtext = Trim(Str"0"(Val(txtopid.Text)))

???
Author
11 May 2007 6:56 PM
Saga
strText = Trim(Str(Val(txtBoxName.Text)))

You start out wih (say) 003 - this is a string
Val() converts the string into a number, resulting in 3
Str() converts the number back to string, adding a extra space in front
    (indicating a positive number)
Trim() gets rid of the leading space

VB does all the work for you, no need to explicitly specify the 0.

You could tweak Grand's line like so:

strText = Cstr(Val(txtBoxName.Text))

Cstr() does not add the leading space for positive numbers.

Regards
Saga



Show quote
"jonny" <nelsonj4NON@SPAMMEhotmail.com> wrote in message
news:1178908845.663980.228690@h2g2000hsg.googlegroups.com...
> On May 11, 1:33 pm, Grand_Poobah
> <iss_boss.del***@delete.sbcglobal.net> wrote:
>> strText = Trim(Str(Val(txtBoxName.Text)))
>>
>> GP
>>
>> --->
>>
>>
>>
>> > I am trying to trim (remove) off leading zero's from a number in a
>> > text box. The number may be anything from 0001 to 0999.
>>
>> > Is this possible? Your help is greatly appreciated.- Hide quoted text -
>>
>> - Show quoted text -
>
> Thanks Grand. Where do I tell it to remove zeros.
>
>
> Dim strtext As String
> strtext = Trim(Str"0"(Val(txtopid.Text)))
>
> ???
>
>
Author
11 May 2007 7:05 PM
Rick Rothstein (MVP - VB)
>> > I am trying to trim (remove) off leading zero's from a number in a
>> > text box. The number may be anything from 0001 to 0999.
>>
>> > Is this possible? Your help is greatly appreciated.- Hide quoted text -
>>
>> strText = Trim(Str(Val(txtBoxName.Text)))
>
> Thanks Grand. Where do I tell it to remove zeros.
>
> Dim strtext As String
> strtext = Trim(Str"0"(Val(txtopid.Text)))

You changed what Grand_Poobah posted... the code he posted removes the
zeroes as posted. the Val function changes the string of digits into a real
number (real numbers do not have any format, so the leading zeroes won't be
present); the Str function changes the resulting number to a String value;
the Trim function removes the leading blank space that the Str function
includes on positive numbers.

The only problem you may face with Grand_Poobah's code is if TextBox entry
has a decimal point in it and your Regional Settings specify something other
than a dot for the decimal separator (Val only recognizes the dot as the
decimal separator). The following code will remove leading zeroes and also
respect whatever the decimal separator is on the computer running your
code...

Dim strtext As String
strtext = CStr(CDbl(txtopid.Text))

Rick
Author
11 May 2007 8:34 PM
Jeff Johnson
"Rick Rothstein (MVP - VB)" <rickNOSPAMnews@NOSPAMcomcast.net> wrote in
message news:%23xhbT9$kHHA.2552@TK2MSFTNGP06.phx.gbl...

> The following code will remove leading zeroes and also respect whatever
> the decimal separator is on the computer running your code...
>
> Dim strtext As String
> strtext = CStr(CDbl(txtopid.Text))

....but it will throw an error if the text box contains something that can't
be turn into a number, so have error trapping on.
Author
11 May 2007 7:05 PM
Robert Morley
Here's a slightly shorter variant of GP's method:

    strText = CStr(Val(txtBoxName.Text))

The Val is what strips the 0's, in that it evaluates the string and converts
it to a number.  Then the CStr converts it back to a string.  CStr has the
advantage that it doesn't include a leading space.


Rob

Show quote
"jonny" <nelso***@hotmail.com> wrote in message
news:1178908845.663980.228690@h2g2000hsg.googlegroups.com...
> On May 11, 1:33 pm, Grand_Poobah
> <iss_boss.del***@delete.sbcglobal.net> wrote:
>> strText = Trim(Str(Val(txtBoxName.Text)))
>>
>> GP
>>
>> --->
>>
>>
>>
>> > I am trying to trim (remove) off leading zero's from a number in a
>> > text box. The number may be anything from 0001 to 0999.
>>
>> > Is this possible? Your help is greatly appreciated.- Hide quoted text -
>>
>> - Show quoted text -
>
> Thanks Grand. Where do I tell it to remove zeros.
>
>
> Dim strtext As String
> strtext = Trim(Str"0"(Val(txtopid.Text)))
>
> ???
>
>
Author
11 May 2007 9:54 PM
MikeD
Show quote
"jonny" <nelso***@hotmail.com> wrote in message
news:1178908845.663980.228690@h2g2000hsg.googlegroups.com...
> On May 11, 1:33 pm, Grand_Poobah
> <iss_boss.del***@delete.sbcglobal.net> wrote:
>> strText = Trim(Str(Val(txtBoxName.Text)))
>>
>> GP
>>
>> --->
>>
>>
>>
>> > I am trying to trim (remove) off leading zero's from a number in a
>> > text box. The number may be anything from 0001 to 0999.
>>
>> > Is this possible? Your help is greatly appreciated.- Hide quoted text -
>>
>> - Show quoted text -
>
> Thanks Grand. Where do I tell it to remove zeros.
>
>
> Dim strtext As String
> strtext = Trim(Str"0"(Val(txtopid.Text)))
>


All these answers with Val, Trim, Str, CStr, etc.  What's wrong with this?

strtext = Format$(txtopid.Text, "####")


--
Mike
Microsoft Visual Basic MVP
Author
11 May 2007 10:05 PM
Grand_Poobah
In over 40 years of programming, I've learned that there is absolutely
more than one way to do something. :<)

Want 10 answers, ask 10 programmers. My hat is off to all of them.

GP

--->
Show quote
> "jonny" <nelso***@hotmail.com> wrote in message
> news:1178908845.663980.228690@h2g2000hsg.googlegroups.com...
>> On May 11, 1:33 pm, Grand_Poobah
>> <iss_boss.del***@delete.sbcglobal.net> wrote:
>>> strText = Trim(Str(Val(txtBoxName.Text)))
>>>
>>> GP
>>>
>>> --->
>>>
>>>
>>>
>>>> I am trying to trim (remove) off leading zero's from a number in a
>>>> text box. The number may be anything from 0001 to 0999.
>>>> Is this possible? Your help is greatly appreciated.- Hide quoted text -
>>> - Show quoted text -
>> Thanks Grand. Where do I tell it to remove zeros.
>>
>>
>> Dim strtext As String
>> strtext = Trim(Str"0"(Val(txtopid.Text)))
>>
>
>
> All these answers with Val, Trim, Str, CStr, etc.  What's wrong with this?
>
> strtext = Format$(txtopid.Text, "####")
>
>
Author
11 May 2007 10:25 PM
Bob Butler
"Grand_Poobah" <iss_boss.del***@delete.sbcglobal.net> wrote in message
news:%235gJ3hBlHHA.4248@TK2MSFTNGP06.phx.gbl...
> In over 40 years of programming, I've learned that there is absolutely
> more than one way to do something. :<)
>
> Want 10 answers, ask 10 programmers. My hat is off to all of them.

For 10 answers it's usually enough to ask 4-5 programmers.  2-3 if they've
been doing it for a while.  1 if he's really good! <g>
Author
12 May 2007 12:17 AM
Rick Rothstein (MVP - VB)
>> In over 40 years of programming, I've learned that there is absolutely
>> more than one way to do something. :<)
>>
>> Want 10 answers, ask 10 programmers. My hat is off to all of them.
>
> For 10 answers it's usually enough to ask 4-5 programmers.  2-3 if they've
> been doing it for a while.  1 if he's really good! <g>

And if he is bad, he might come up with something like this...

strtext = Replace(Trim$(Replace(txtopid.Text, "0", " ")), " ", "0")

Rick
Author
12 May 2007 12:25 AM
Rick Rothstein (MVP - VB)
>>> In over 40 years of programming, I've learned that there is absolutely
>>> more than one way to do something. :<)
>>>
>>> Want 10 answers, ask 10 programmers. My hat is off to all of them.
>>
>> For 10 answers it's usually enough to ask 4-5 programmers.  2-3 if
>> they've been doing it for a while.  1 if he's really good! <g>
>
> And if he is bad, he might come up with something like this...
>
> strtext = Replace(Trim$(Replace(txtopid.Text, "0", " ")), " ", "0")

Hmm! I just had a thought which might make a slight modification of this
one-liner more desirable... what if the number being reformatted was a
floating point value which had trailing zeroes (indicating, say, measured
precision) which were to be retained? Then the following would work, whereas
I'm not so sure any of the other submissions would...

strtext = Replace(LTrim$(Replace(txtopid.Text, "0", " ")), " ", "0")


Rick
Author
12 May 2007 1:59 AM
Bob Butler
Show quote
"Rick Rothstein (MVP - VB)" <rickNOSPAMnews@NOSPAMcomcast.net> wrote in
message news:uwwrJwClHHA.3996@TK2MSFTNGP06.phx.gbl...
>>>> In over 40 years of programming, I've learned that there is absolutely
>>>> more than one way to do something. :<)
>>>>
>>>> Want 10 answers, ask 10 programmers. My hat is off to all of them.
>>>
>>> For 10 answers it's usually enough to ask 4-5 programmers.  2-3 if
>>> they've been doing it for a while.  1 if he's really good! <g>
>>
>> And if he is bad, he might come up with something like this...
>>
>> strtext = Replace(Trim$(Replace(txtopid.Text, "0", " ")), " ", "0")
>
> Hmm! I just had a thought which might make a slight modification of this
> one-liner more desirable... what if the number being reformatted was a
> floating point value which had trailing zeroes (indicating, say, measured
> precision) which were to be retained? Then the following would work,
> whereas I'm not so sure any of the other submissions would...
>
> strtext = Replace(LTrim$(Replace(txtopid.Text, "0", " ")), " ", "0")

LOL
Author
12 May 2007 2:12 AM
Rick Rothstein (MVP - VB)
>> strtext = Replace(LTrim$(Replace(txtopid.Text, "0", " ")), " ", "0")
>
> LOL

What... what is so funny? <g>

Rick
Author
11 May 2007 10:24 PM
Bob Butler
"MikeD" <nob***@nowhere.edu> wrote in message
news:OixkAcBlHHA.1776@TK2MSFTNGP05.phx.gbl...
> All these answers with Val, Trim, Str, CStr, etc.  What's wrong with this?
>
> strtext = Format$(txtopid.Text, "####")

Beyond the fact that it uses implicit type conversions so you're letting VB
guess at how to do the conversions?  nothing.
Author
11 May 2007 11:58 PM
Rick Rothstein (MVP - VB)
> All these answers with Val, Trim, Str, CStr, etc.  What's wrong with this?
>
> strtext = Format$(txtopid.Text, "####")

The main problem I see is it will only work for integer values (although
that may be all the OP wants).

Rick
Author
12 May 2007 12:02 AM
Rick Rothstein (MVP - VB)
>> strtext = Format$(txtopid.Text, "####")
>
> The main problem I see is it will only work for integer values (although
> that may be all the OP wants).

I forgot to say.... if you left off the format specification pattern string,
then it would have been fine.

strtext = Format$(txtopid.Text)

Rick
Author
12 May 2007 2:03 AM
Robert Morley
> The main problem I see is it will only work for integer values (although
> that may be all the OP wants).

That would seem to be implied in the given range of "0001" to "0999", though
certainly not absolutely guaranteed.  (For that matter, if that's the range,
one wonders why there's a leading "0" on all of them, but whatever. :-)



Rob
Author
12 May 2007 2:00 AM
Robert Morley
> All these answers with Val, Trim, Str, CStr, etc.  What's wrong with this?
>
> strtext = Format$(txtopid.Text, "####")

Nothing, though it's probably the slowest way of doing it (not that it's
likely to matter if you're doing this on a data entry form that's probably
updated a lot less than a million or so times per second <g>).

My results, based on the code below (in sec.):

Format: 12.41
Trim/Str/Val: 11.52
CStr/Val: 7.50
CStr/CDbl: 4.41
CStr/CInt: 2.66
CStr/CLng: 2.39


Public Sub TestIt()
    Const NumIter As Long = 5000000
    Const StringNum As String = "0999"
    'Used a constant value, to focus times on the methods involved, rather
    'than extraneous code.  Times seem to vary a bit based on the string
    'you use, but not significantly.

    Dim i As Long
    Dim s As String
    Dim t As Double

    t = Timer
    For i = 1 To NumIter
        s = Format(StringNum, "####")
    Next
    Debug.Print "Format: " & Format(Timer - t, "0.00")

    t = Timer
    For i = 1 To NumIter
        s = Trim(Str(Val(StringNum)))
    Next
    Debug.Print "Trim/Str/Val: " & Format(Timer - t, "0.00")

    t = Timer
    For i = 1 To NumIter
        s = CStr(Val(StringNum))
    Next
    Debug.Print "CStr/Val: " & Format(Timer - t, "0.00")

    t = Timer
    For i = 1 To NumIter
        s = CStr(CDbl(StringNum))
    Next
    Debug.Print "CStr/CDbl: " & Format(Timer - t, "0.00")

    t = Timer
    For i = 1 To NumIter
        s = CStr(CInt(StringNum))
    Next
    Debug.Print "CStr/CInt: " & Format(Timer - t, "0.00")

    t = Timer
    For i = 1 To NumIter
        s = CStr(CLng(StringNum))
    Next
    Debug.Print "CStr/CLng: " & Format(Timer - t, "0.00")
End Function
Author
12 May 2007 2:10 AM
Rick Rothstein (MVP - VB)
See inline comments....

>> All these answers with Val, Trim, Str, CStr, etc.  What's wrong with
>> this?
>>
>> strtext = Format$(txtopid.Text, "####")
>
> Nothing, though it's probably the slowest way of doing it (not that it's
> likely to matter if you're doing this on a data entry form that's probably
> updated a lot less than a million or so times per second <g>).
>
> My results, based on the code below (in sec.):
>
> Format: 12.41

How does Format do if you leave off the format specification pattern? That
is, just use Format$(txttopid.Text).

> Trim/Str/Val: 11.52
> CStr/Val: 7.50
> CStr/CDbl: 4.41

Remember... the above was meant to handle both integer and floating point
numbers... the code below are only good for integer values.

> CStr/CInt: 2.66
> CStr/CLng: 2.39

Rick
Author
12 May 2007 2:26 AM
Robert Morley
> How does Format do if you leave off the format specification pattern? That
> is, just use Format$(txttopid.Text).

....he asks me right after I delete my test code. :-)

Not surprisingly, it did better.  What was surprising was just how much
better:  6.94 seconds compared to the original 12.41.  (Oh and "D'oh!  Why
didn't I think of that?")

> Remember... the above was meant to handle both integer and floating point
> numbers... the code below are only good for integer values.

Yes, I was aware of that, which is why I tested both.  My assumption was
that the OP was only dealing with integer values, though that was far from
guaranteed.

(PS, all my numbers were running out of Access VBA, cuz that's where I'm
used to programming, but I suspect you'd see nearly identical numbers out of
VB6, though the various compile options might make a significant difference
in some cases.)

(PPS, in copying the original from my message when I was re-testing, I
noticed that I had Public Sub/End Function...my bad for changing the code
slightly in my post.)



Rob

AddThis Social Bookmark Button