Str Function in VBA for Excel adds a character to

2020-04-08 01:26发布

问题:

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?

回答1:

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) = " "


回答2:

Easiest way to find out:

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


回答3:

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