Name
Str Function
Class
Microsoft.VisualBasic.Conversion
Syntax
Str(number
)
-
number
Use: Required
Data Type: Numeric
Any valid numeric expression or expression capable of conversion to a number
Return Value
A String representation of number
Description
Converts number
from a numeric to a string
Rules at a Glance
If the conversion of
number
to a string cannot be made, an InvalidCastException error occurs. To prevent this, you can check the value ofnumber
by passing it to theIsNumeric
function before callingStr
.If
number
is not a numeric value or is not capable of conversion to a number (so that it can in turn be converted to a string), an InvalidCastException exception occurs.If the return value is positive, the
Str
function always includes a leading space in the returned string for the sign ofnumber
.
Programming Tips and Gotchas
Use the
LTrim
function to remove the leading space that theStr
function adds to the start of the returned string.Both the
CStr
andFormat
functions have now superceded theStr
function. TheCStr
function does not add a leading space for the sign of a positive number. Both theCStr
and theFormat
functions are internationally aware, able to recognize decimal delimiters other than the period (.).
See Also
Get VB .NET Language in a Nutshell now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.