5

I am converting an integer to string using the str() function

However, I noticed that the str() function would return an extra character to the string.

For example, MsgBox(Len(str(1))) would return 2.

What is the extra character being appended?

3
  • 2
    Doug Glancy's quote is correct although I would have added the next line: "Use the Format function to convert numeric values you want formatted as dates, times, or currency or in other user-defined formats. Unlike Str, the Format function doesn't include a leading space for the sign of number." [My italics] Commented Apr 4, 2012 at 8:44
  • 1
    And if you want to trim that extra blank space, you can always use this MsgBox (Len(Trim(Str(1)))) Commented Apr 4, 2012 at 10:27
  • 1
    Use Cstr(1) rather than Str(1). Cstr does not add a space. Commented Feb 24, 2021 at 23:57

3 Answers 3

10

From Excel 2010 help:

"When numbers are converted to strings, a leading space is always reserved for the sign of number. If number is positive, the returned string contains a leading space and the plus sign is implied."

And sure enough this statement returns True in the debug window:

? left(str(1),1) = " "
Sign up to request clarification or add additional context in comments.

Comments

0

Easiest way to find out:

MsgBox(Asc(Right(Str(1),1)))

Comments

0

As pointed out in this answer you should use the format() function.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.