Xbasic

STR Function

Syntax

Formatted_String as C = STR(N number[,N length[,N decimal_places[,C number_format]]])

Arguments

number

The number to format. Numeric

length

Optional. Default (10). The number of characters in the Formatted_String. Refer to Number Formats for more information.

decimal_places

Optional. Default (0). The number of characters after the decimal point in the Formatted_String. If you do not specify decimal places, STR() rounds the Number to the nearest whole number.

number_format

Default (""). The pattern to use when formatting Formatted_String. Values for Format are:

Format Character
Meaning
$

Includes a leading currency symbol.

%

Displays a number as a percentage.

,

Separates thousands with the thousands separator.

*

Pad with leading '*' characters instead of spaces.

X

Formats the number in words (e.g. for checkbook applications).

L

Use lowercase (useful when formatting as words).

W

Use propercase (first character uppercase, rest of word is lowercase).

-

Use to change to position of the '-' for negavtive values.

(

Use parentheses to represent negative numbers.

B

Blank if zero format.

Description

Converts a number to a character string.

Discussion

STR() converts a Number to a right-justified character string. The optional Length parameter determines the length of the string, and the optional Decimal_Places parameter specifies the number of decimal places. The length must allow space for decimals, the decimal point, and a - or + sign. Note : Because STR() returns a string with a right-justified number, you may want to use the LTRIM() function to remove leading spaces before using the string in other operations.

Example

str(SALARY) -> "     25500", if SALARY contains 25500
str(SALARY/10000,8,4) -> "    2.5500"
str(23434.34,12,2,"$,") -> " $23,434.34"

You may need to order the records in a table on multiple fields that are of different types. For example, you may want to create an index to display records in DEPARTMENT order, and within DEPARTMENT by SALARY.

DEPARTMENT + str(SALARY)

Alpha Anywhere enables you to create new text strings by concatenating existing strings. The individual pieces to be concatenated must all be character strings. You, therefore, have to convert numeric fields to character strings before they can be concatenated to other fields. The following expression, for example, returns a date in the format "Month DD, YYYY" (e.g., November 11, 1968).

cmonth(BIRTHDATE) + " " + ltrimday(BIRTHDATE?) + ", " + ltrimyear(BIRTHDATE?)

STR format specs by example 

Format Spec
Example Output
"B"

123456.78 [blank if zero]

"$("

$123,456.78

"-$"

-$123,456.78

"@("

$ 123,456.78

","

123,456.78

",("

(123,456.78)

"%"

123,456.78%

"XF"

Fifty six and 78/100

"XF$"

Fifty six and 78/100 Dollars

"$(B"

$123,456.78 [blank if zero]

",B"

123,456.78 [blank if zero]

"%B"

123,456.78% [blank if zero]

"XBF"

Fifty six and 78/100 [blank if zero]

"X$(BF"

Fifty six and 78/100 Dollars [blank if zero]

"$(*"

***$123,456.78

",*"

***123,456.78

"X*F"

***Fifty six and 78/100

"X$(*F"

***Fifty six and 78/100 Dollars

"$(B*"

***$123,456.78 [blank if zero]

",B*"

***123,456.78 [blank if zero]

"XB*F"

***Fifty six and 78/100 [blank if zero]

"X$(B*F"

***Fifty six and 78/100 Dollars [blank if zero]

"XC"

FIFTY SIX AND 78/100

"X$(C"

FIFTY SIX AND 78/100 DOLLARS

"XBC"

FIFTY SIX AND 78/100 [blank if zero]

"X$(BC"

FIFTY SIX AND 78/100 DOLLARS [blank if zero]

"X*C"

***FIFTY SIX AND 78/100

"X$(*C"

***FIFTY SIX AND 78/100 DOLLARS

"XB*C"

***FIFTY SIX AND 78/100 [blank if zero]

"X$(B*C"

***FIFTY SIX AND 78/100 DOLLARS [blank if zero]

"XL"

fifty six and 78/100

"X$(L"

fifty six and 78/100 dollars

"XBL"

fifty six and 78/100 [blank if zero]

"X$(BL"

fifty six and 78/100 dollars [blank if zero]

"X*L"

***fifty six and 78/100

"X$(*L"

***fifty six and 78/100 dollars

"XB*L"

***fifty six and 78/100 [blank if zero]

"X$(B*L"

***fifty six and 78/100 dollars [blank if zero]

"XW"

Fifty Six And 78/100

"X$(W"

Fifty Six And 78/100 Dollars

"XBW"

Fifty Six And 78/100 [blank if zero]

"X$(BW"

Fifty Six And 78/100 Dollars[blank if zero]

"X*W"

***Fifty Six And 78/100

"X$(*W"

***Fifty Six And 78/100 Dollars

"XB*W"

***Fifty Six And 78/100 [blank if zero]

"X$(B*W"

***Fifty Six And 78/100 Dollars [blank if zero]

"Uact0"

Accounting ' 234,123.23 ', ' (234,123.23)', ' - '

"Uact1"

Accounting '$ 234,123.23 ', '$ (234,123.23)', ' - '

See Also