Converting numeric to string

G

Guest

I have found in VBA that a simple assignment statement to convert a numeric to a string results in a blank character being added to the beginning of the string
e.g
intNum = 23
strResult = str$(intNum
result is strResult = " 234" instead of "234

Why does VBA do this? What is the easiest way (least code) to strip the beginning blank

ctdak
 
P

Patrick

HI!!
When I do converts between strings and Integers I usually
use things like: Cstr(IntNum) or Cint(Str) and I've never
noticed if an extra space is entered (when converting a
number) perhaps I'ved not payed enough attention to it....

Please let me know if this makes any sence at all!!
Else I would propably write something like:

intNum = 234
strResult = Cstr(intNum)
In this case, it should return a "234".

If you want a list of this functions, just press 'F1' on
the Cstr or Cint.

HTH,
Patrick
-----Original Message-----
I have found in VBA that a simple assignment statement to
convert a numeric to a string results in a blank character
being added to the beginning of the string.
e.g.
intNum = 234
strResult = str$(intNum)
result is strResult = " 234" instead of "234"

Why does VBA do this? What is the easiest way (least
code) to strip the beginning blank?
 
J

Jim Allensworth

I have found in VBA that a simple assignment statement to convert a numeric to a string results in a blank character being added to the beginning of the string.
e.g.
intNum = 234
strResult = str$(intNum)
result is strResult = " 234" instead of "234"

Why does VBA do this? What is the easiest way (least code) to strip the beginning blank?

ctdak

That space is reserved for sign of the number with positive numbers a
space is returned as the plus sign is implied.

Use the Format function to return a string without the sign space.

strResult = Format(intNum)


- Jim
 
V

Van T. Dinh

Str() and Str$() *always* reserve a leading character for the sign. If the
number is +ve, the character is a space. This is pointed out in Access VBA
Help on Str().

?Format(123, "0")
123 ***no leading space

There are a number of other methods but I prefer to be specific like above.

HTH
Van T. Dinh
MVP (Access)



ctdak said:
I have found in VBA that a simple assignment statement to convert a
numeric to a string results in a blank character being added to the
beginning of the string.
 
G

Guest

Patrick
Yes this makes sense. I will use the conversion function from now on. It's interesting that the blank character I was getting at the beginning of the string was due to a +/- sign where a + is left as a blank
Thanks for answering. I had forgotten about the conversion functions
ctda

----- Patrick wrote: ----

HI!
When I do converts between strings and Integers I usually
use things like: Cstr(IntNum) or Cint(Str) and I've never
noticed if an extra space is entered (when converting a
number) perhaps I'ved not payed enough attention to it...

Please let me know if this makes any sence at all!
Else I would propably write something like

intNum = 23
strResult = Cstr(intNum
In this case, it should return a "234"

If you want a list of this functions, just press 'F1' on
the Cstr or Cint

HTH
Patric
-----Original Message----
I have found in VBA that a simple assignment statement to
convert a numeric to a string results in a blank character
being added to the beginning of the string
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top