Discussion:
How can I trim off leading zeros from a number? Help greatly appreciated!!!
(too old to reply)
jonny
2007-05-11 18:26:45 UTC
Permalink
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.
Grand_Poobah
2007-05-11 18:33:12 UTC
Permalink
strText = Trim(Str(Val(txtBoxName.Text)))

GP

--->
Post by 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.
jonny
2007-05-11 18:40:45 UTC
Permalink
On May 11, 1:33 pm, Grand_Poobah
Post by Grand_Poobah
strText = Trim(Str(Val(txtBoxName.Text)))
GP
--->
Post by 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.- 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)))

???
Saga
2007-05-11 18:56:33 UTC
Permalink
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
Post by jonny
On May 11, 1:33 pm, Grand_Poobah
Post by Grand_Poobah
strText = Trim(Str(Val(txtBoxName.Text)))
GP
--->
Post by 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.- 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)))
???
jonny
2007-05-21 18:21:19 UTC
Permalink
Post by Grand_Poobah
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.
strText = Cstr(Val(txtBoxName.Text))
Cstr() does not add the leading space for positive numbers.
Regards
Saga
Post by jonny
On May 11, 1:33 pm, Grand_Poobah
Post by Grand_Poobah
strText = Trim(Str(Val(txtBoxName.Text)))
GP
--->
Post by 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.- 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)))
???- Hide quoted text -
- Show quoted text -
So far I can not get this to work. I added a button and text box to a
form. On the click of the button I put the following code:

Dim strtext As String

strtext = Trim(Str(Val(Text1.Text)))

In the text1 I put "001" and clicked the button + the zeros did not
get removed?
Larry Serflaten
2007-05-21 18:33:16 UTC
Permalink
Post by jonny
So far I can not get this to work. I added a button and text box to a
Dim strtext As String
strtext = Trim(Str(Val(Text1.Text)))
In the text1 I put "001" and clicked the button + the zeros did not
get removed?
They did get removed, but the result was stored in a string variable,
and not back into the textbox.

Try this in your button:

Text1.Text = Cstr(Val(Text1.Text))

LFS
Mike Williams
2007-05-21 18:42:47 UTC
Permalink
Post by jonny
So far I can not get this to work. I added a button and
text box to a form. On the click of the button I put the
Dim strtext As String
strtext = Trim(Str(Val(Text1.Text)))
In the text1 I put "001" and clicked the button + the zeros
did not get removed?
It's often difficult to judge the level of expertise of someone who posts a
question, which sometimes means that a respondent does not point out
something he personally considers to be self evident, even though it may not
actually be self evident to the OP. Anyway, having said that, you are not
seeing the result reflected in the Text Box because you are not actually
placing the result into the TextBox. In the example code you are merely
"working out the result" and placing it into the String variable called
strtext. Unless you do something else with the result then that is where it
stays (in the variable). In order to get the result reflected in the Text
Box you need to actually place it there. For example, try the following
slight modification to your existing code:

Dim strtext As String
strtext = Trim(Str(Val(Text1.Text)))
Text1.Text = strtext

As you will see, the third line of that code transfers the result to the
Text Box, replacing what was originally there, so that you can see it. Even
better would be to omit the strtext variable altogether and do the complete
job in just one line of code, as in the following example:

Text1.Text = Trim(Str(Val(Text1.Text)))

Does that help at all?

Mike
jonny
2007-05-21 21:11:37 UTC
Permalink
Post by Mike Williams
Post by jonny
So far I can not get this to work. I added a button and
text box to a form. On the click of the button I put the
Dim strtext As String
strtext = Trim(Str(Val(Text1.Text)))
In the text1 I put "001" and clicked the button + the zeros
did not get removed?
It's often difficult to judge the level of expertise of someone who posts a
question, which sometimes means that a respondent does not point out
something he personally considers to be self evident, even though it may not
actually be self evident to the OP. Anyway, having said that, you are not
seeing the result reflected in the Text Box because you are not actually
placing the result into the TextBox. In the example code you are merely
"working out the result" and placing it into the String variable called
strtext. Unless you do something else with the result then that is where it
stays (in the variable). In order to get the result reflected in the Text
Box you need to actually place it there. For example, try the following
Dim strtext As String
strtext = Trim(Str(Val(Text1.Text)))
Text1.Text = strtext
As you will see, the third line of that code transfers the result to the
Text Box, replacing what was originally there, so that you can see it. Even
better would be to omit the strtext variable altogether and do the complete
Text1.Text = Trim(Str(Val(Text1.Text)))
Does that help at all?
Mike
THANK YOU SO MUCH!!!! THIS WORKS GREAT.
Phill W.
2007-05-22 12:41:11 UTC
Permalink
Post by jonny
Post by Mike Williams
Text1.Text = Trim(Str(Val(Text1.Text)))
THANK YOU SO MUCH!!!! THIS WORKS GREAT.
And now you've got all of that sorted, forget about Str(), which is a
pain in the proverbial with that leading space, and use Format() instead:

Text1.Text = Format(Val(Text1.Text))

HTH,
Phill W.
Rick Rothstein (MVP - VB)
2007-05-22 13:07:13 UTC
Permalink
Post by jonny
Post by Mike Williams
Text1.Text = Trim(Str(Val(Text1.Text)))
THANK YOU SO MUCH!!!! THIS WORKS GREAT.
And now you've got all of that sorted, forget about Str(), which is a pain
Text1.Text = Format(Val(Text1.Text))
Do you really need the Val function call if all you are doing is removing
leading zeroes?

Rick
Bob Butler
2007-05-22 13:16:53 UTC
Permalink
Post by Rick Rothstein (MVP - VB)
Post by Phill W.
Post by jonny
Post by Mike Williams
Text1.Text = Trim(Str(Val(Text1.Text)))
THANK YOU SO MUCH!!!! THIS WORKS GREAT.
And now you've got all of that sorted, forget about Str(), which is a
Text1.Text = Format(Val(Text1.Text))
Do you really need the Val function call if all you are doing is removing
leading zeroes?
Yes, to avoid implicit type conversions. I would, however, use CStr, not
Format.
Rick Rothstein (MVP - VB)
2007-05-22 15:10:55 UTC
Permalink
Post by Bob Butler
Post by Rick Rothstein (MVP - VB)
Post by Phill W.
Post by jonny
Post by Mike Williams
Text1.Text = Trim(Str(Val(Text1.Text)))
THANK YOU SO MUCH!!!! THIS WORKS GREAT.
And now you've got all of that sorted, forget about Str(), which is a
Text1.Text = Format(Val(Text1.Text))
Do you really need the Val function call if all you are doing is removing
leading zeroes?
Yes, to avoid implicit type conversions. I would, however, use CStr, not
Format.
I'm not sure I see the danger in implicit type conversion with the Format
function, at least not as it is being used here. I usually associate that
danger when chained calculations are involved. The Val function takes a text
representation of a number and converts it into a number... so does the
Format function. Well, actually, the Format function sticks the converted
String value into a Variant data type for return, but I am not so sure the
Val function doesn't do the same. From the Val function's help page... [the
Val function] "returns the numbers contained in a string as a numeric value
of appropriate type". Appropriate type? Sounds like a Variant at work
underneath to me. As for the CStr part... tacking on a $ symbol onto the
Format function takes care of that...

Text1.Text = Format$(Text1.Text)

Rick
Bob Butler
2007-05-22 16:21:18 UTC
Permalink
Post by Rick Rothstein (MVP - VB)
Post by Bob Butler
Post by Rick Rothstein (MVP - VB)
Post by Phill W.
Post by jonny
Post by Mike Williams
Text1.Text = Trim(Str(Val(Text1.Text)))
THANK YOU SO MUCH!!!! THIS WORKS GREAT.
And now you've got all of that sorted, forget about Str(), which is a
Text1.Text = Format(Val(Text1.Text))
Do you really need the Val function call if all you are doing is
removing leading zeroes?
Yes, to avoid implicit type conversions. I would, however, use CStr, not
Format.
I'm not sure I see the danger in implicit type conversion with the Format
function,
It's not that there is a "danger", it's that Val indicates clearly that the
programmer knows that this is a string that is being converted to a number.
Format only indicates that some sort of formatting is wanted on whatever the
parameter is.

Using Format without specifying the format to use looks to me like the
programmer said "take whatever this is and do something with it" and that's
hardly a controlled operation.
Rick Rothstein (MVP - VB)
2007-05-22 16:53:28 UTC
Permalink
Post by Bob Butler
Format only indicates that some sort of formatting is wanted on whatever
the parameter is.
Using Format without specifying the format to use looks to me like the
programmer said "take whatever this is and do something with it" and
that's hardly a controlled operation.
Using Format without specifying a format is documented... type Format
somewhere in VB and hit the F1 key... if you scroll down to the first
paragraph of text, this is what you will see...

"If you try to format a number without specifying format, Format
provides functionality similar to the Str function, although it is
internationally aware. However, positive numbers formatted as
strings using Format don't include a leading space reserved for
the sign of the value; those converted using Str retain the leading
space."

Rick
Bob Butler
2007-05-22 17:14:03 UTC
Permalink
Post by Rick Rothstein (MVP - VB)
Post by Bob Butler
Format only indicates that some sort of formatting is wanted on whatever
the parameter is.
Using Format without specifying the format to use looks to me like the
programmer said "take whatever this is and do something with it" and
that's hardly a controlled operation.
Using Format without specifying a format is documented... type Format
somewhere in VB and hit the F1 key... if you scroll down to the first
paragraph of text, this is what you will see...
"If you try to format a number without specifying format, Format
provides functionality similar to the Str function, although it is
internationally aware. However, positive numbers formatted as
strings using Format don't include a leading space reserved for
the sign of the value; those converted using Str retain the leading
space."
but you aren't formatting a number, you are formatting a string that
contains a number and relying on the implicit type conversion

it's not a big deal, more just a personal preference
Rick Raisley
2007-05-23 14:16:48 UTC
Permalink
Post by Rick Rothstein (MVP - VB)
Post by Bob Butler
Post by Rick Rothstein (MVP - VB)
Post by Phill W.
Post by jonny
Post by Mike Williams
Text1.Text = Trim(Str(Val(Text1.Text)))
THANK YOU SO MUCH!!!! THIS WORKS GREAT.
And now you've got all of that sorted, forget about Str(), which is a
Text1.Text = Format(Val(Text1.Text))
Do you really need the Val function call if all you are doing is removing
leading zeroes?
Yes, to avoid implicit type conversions. I would, however, use CStr, not
Format.
I'm not sure I see the danger in implicit type conversion with the Format
function, at least not as it is being used here. I usually associate that
danger when chained calculations are involved. The Val function takes a text
representation of a number and converts it into a number... so does the
Format function. Well, actually, the Format function sticks the converted
String value into a Variant data type for return, but I am not so sure the
Val function doesn't do the same. From the Val function's help page... [the
Val function] "returns the numbers contained in a string as a numeric value
of appropriate type". Appropriate type? Sounds like a Variant at work
underneath to me. As for the CStr part... tacking on a $ symbol onto the
Format function takes care of that...
Text1.Text = Format$(Text1.Text)
My concern with Val is that it is not Internationally aware. So if the text
were "1,234" it would always result in 1234 regardless of the International
settings. Likewise, I'm not that fond of Csng or similar functions, as they
can't handle non-numbers, so require error-checking, making a one-liner a
bit difficult.

Actually, the above, not using either, might get around both of those. ;-)
--
Regards,

Rick Raisley
Bob Butler
2007-05-23 16:36:06 UTC
Permalink
"Rick Raisley" <heavymetal-A-T-bellsouth-D-O-T-net> wrote in message news:***@TK2MSFTNGP05.phx.gbl...
<cut>
making a one-liner a bit difficult.
no offense but that's not usually a goal <g>
Rick Rothstein (MVP - VB)
2007-05-23 17:03:07 UTC
Permalink
Post by Bob Butler
making a one-liner a bit difficult.
no offense but that's not usually a goal <g>
Really?!!?? <vbg>

Rick

Rick Rothstein (MVP - VB)
2007-05-11 19:05:17 UTC
Permalink
Post by jonny
Post by Grand_Poobah
Post by 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.- 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
Jeff Johnson
2007-05-11 20:34:13 UTC
Permalink
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.
Robert Morley
2007-05-11 19:05:47 UTC
Permalink
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
Post by jonny
On May 11, 1:33 pm, Grand_Poobah
Post by Grand_Poobah
strText = Trim(Str(Val(txtBoxName.Text)))
GP
--->
Post by 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.- 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)))
???
MikeD
2007-05-11 21:54:38 UTC
Permalink
Post by jonny
On May 11, 1:33 pm, Grand_Poobah
Post by Grand_Poobah
strText = Trim(Str(Val(txtBoxName.Text)))
GP
--->
Post by 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.- 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
Grand_Poobah
2007-05-11 22:05:49 UTC
Permalink
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

--->
Post by MikeD
Post by jonny
On May 11, 1:33 pm, Grand_Poobah
Post by Grand_Poobah
strText = Trim(Str(Val(txtBoxName.Text)))
GP
--->
Post by 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.- 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, "####")
Bob Butler
2007-05-11 22:25:01 UTC
Permalink
Post by 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.
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>
Rick Rothstein (MVP - VB)
2007-05-12 00:17:25 UTC
Permalink
Post by Bob Butler
Post by 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.
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
Rick Rothstein (MVP - VB)
2007-05-12 00:25:19 UTC
Permalink
Post by Rick Rothstein (MVP - VB)
Post by Bob Butler
Post by 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.
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
Bob Butler
2007-05-12 01:59:19 UTC
Permalink
Post by Rick Rothstein (MVP - VB)
Post by Rick Rothstein (MVP - VB)
Post by Bob Butler
Post by 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.
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
Rick Rothstein (MVP - VB)
2007-05-12 02:12:19 UTC
Permalink
Post by Rick Rothstein (MVP - VB)
strtext = Replace(LTrim$(Replace(txtopid.Text, "0", " ")), " ", "0")
LOL
What... what is so funny? <g>

Rick
Bob Butler
2007-05-11 22:24:05 UTC
Permalink
Post by MikeD
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.
Rick Rothstein (MVP - VB)
2007-05-11 23:58:55 UTC
Permalink
Post by MikeD
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
Rick Rothstein (MVP - VB)
2007-05-12 00:02:13 UTC
Permalink
Post by Rick Rothstein (MVP - VB)
Post by MikeD
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
Robert Morley
2007-05-12 02:03:35 UTC
Permalink
Post by Rick Rothstein (MVP - VB)
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
Robert Morley
2007-05-12 02:00:35 UTC
Permalink
Post by MikeD
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
Rick Rothstein (MVP - VB)
2007-05-12 02:10:50 UTC
Permalink
See inline comments....
Post by Robert Morley
Post by MikeD
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>).
Format: 12.41
How does Format do if you leave off the format specification pattern? That
is, just use Format$(txttopid.Text).
Post by Robert Morley
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.
Post by Robert Morley
CStr/CInt: 2.66
CStr/CLng: 2.39
Rick
Robert Morley
2007-05-12 02:26:23 UTC
Permalink
Post by Rick Rothstein (MVP - VB)
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?")
Post by Rick Rothstein (MVP - VB)
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
Continue reading on narkive:
Loading...