|
code
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
How can I trim off leading zeros from a number? Help greatly appreciated!!!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. 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. > On May 11, 1:33 pm, Grand_Poobah
<iss_boss.del***@delete.sbcglobal.net> wrote: Show quote > strText = Trim(Str(Val(txtBoxName.Text))) Thanks Grand. Where do I tell it to remove zeros.> > 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 - Dim strtext As String strtext = Trim(Str"0"(Val(txtopid.Text))) ??? 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))) > > ??? > > >> > I am trying to trim (remove) off leading zero's from a number in a You changed what Grand_Poobah posted... the code he posted removes the >> > 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))) 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 "Rick Rothstein (MVP - VB)" <rickNOSPAMnews@NOSPAMcomcast.net> wrote in ....but it will throw an error if the text box contains something that can't 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)) be turn into a number, so have error trapping on. 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))) > > ??? > >
Show quote
"jonny" <nelso***@hotmail.com> wrote in message All these answers with Val, Trim, Str, CStr, etc. What's wrong with this?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))) > strtext = Format$(txtopid.Text, "####") -- Mike Microsoft Visual Basic MVP 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, "####") > > "Grand_Poobah" <iss_boss.del***@delete.sbcglobal.net> wrote in message For 10 answers it's usually enough to ask 4-5 programmers. 2-3 if they've 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. been doing it for a while. 1 if he's really good! <g> >> In over 40 years of programming, I've learned that there is absolutely And if he is bad, he might come up with something like this...>> 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> strtext = Replace(Trim$(Replace(txtopid.Text, "0", " ")), " ", "0") Rick >>> In over 40 years of programming, I've learned that there is absolutely Hmm! I just had a thought which might make a slight modification of this >>> 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") 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
Show quote
"Rick Rothstein (MVP - VB)" <rickNOSPAMnews@NOSPAMcomcast.net> wrote in LOLmessage 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") >> strtext = Replace(LTrim$(Replace(txtopid.Text, "0", " ")), " ", "0") What... what is so funny? <g>> > LOL Rick "MikeD" <nob***@nowhere.edu> wrote in message Beyond the fact that it uses implicit type conversions so you're letting VB news:OixkAcBlHHA.1776@TK2MSFTNGP05.phx.gbl... > All these answers with Val, Trim, Str, CStr, etc. What's wrong with this? > > strtext = Format$(txtopid.Text, "####") guess at how to do the conversions? nothing. > All these answers with Val, Trim, Str, CStr, etc. What's wrong with this? The main problem I see is it will only work for integer values (although > > strtext = Format$(txtopid.Text, "####") that may be all the OP wants). Rick >> strtext = Format$(txtopid.Text, "####") I forgot to say.... if you left off the format specification pattern string, > > The main problem I see is it will only work for integer values (although > that may be all the OP wants). then it would have been fine. strtext = Format$(txtopid.Text) Rick > The main problem I see is it will only work for integer values (although That would seem to be implied in the given range of "0001" to "0999", though > that may be all the OP wants). 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 > All these answers with Val, Trim, Str, CStr, etc. What's wrong with this? Nothing, though it's probably the slowest way of doing it (not that it's > > strtext = Format$(txtopid.Text, "####") 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 See inline comments....
>> All these answers with Val, Trim, Str, CStr, etc. What's wrong with How does Format do if you leave off the format specification pattern? That >> 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 is, just use Format$(txttopid.Text). > Trim/Str/Val: 11.52 Remember... the above was meant to handle both integer and floating point > CStr/Val: 7.50 > CStr/CDbl: 4.41 numbers... the code below are only good for integer values. > CStr/CInt: 2.66 Rick> CStr/CLng: 2.39 > How does Format do if you leave off the format specification pattern? That ....he asks me right after I delete my test code. :-)> is, just use Format$(txttopid.Text). 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 Yes, I was aware of that, which is why I tested both. My assumption was > numbers... the code below are only good for integer values. 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 |
|||||||||||||||||||||||