Difference between revisions of "SHIP:Sail:toString"

From Serious Documentation
Jump to: navigation, search
(Parameters/Return Value)
Line 23: Line 23:
 
|number||{{DataType|Integer}}||The number (or any expression returning number) to convert
 
|number||{{DataType|Integer}}||The number (or any expression returning number) to convert
 
|-
 
|-
|width||{{DataType|Integer}}||The desired width of the number, including any "-" sign, left padded with "0"
+
|width||{{DataType|Integer}}||The desired width of the number, including any "-" sign
 
|-
 
|-
 
|radix||{{DataType|Integer}}||The radix of the string (10 is decimal, 16 is hex, 2 is binary, etc.)
 
|radix||{{DataType|Integer}}||The radix of the string (10 is decimal, 16 is hex, 2 is binary, etc.)

Revision as of 14:00, 5 December 2012

SHIP Sail Home

SHIP Sail Function Home

toString

Converts a number to a string, with optional radix and optional leading 0 created width.

Prototype

Integer toString(Integer number);

Integer toString(Integer number, Integer width);

Integer toString(Integer number, Integer width, Integer radix);

Parameters/Return Value

Parameter Data Type Description
number Integer The number (or any expression returning number) to convert
width Integer The desired width of the number, including any "-" sign
radix Integer The radix of the string (10 is decimal, 16 is hex, 2 is binary, etc.)
Return String resultant string

Detailed Description

The toString(number) function returns the string equivalent of a number. Optional radix and width parameters allow conversion into a different base (for instance, base 16 hexadecimal) and left-zero padded to a specific width.

Supported radixes are 2,8,10, and 16. Using a non-supported radix reverts to base 10.

If the resulting string is larger than the requested width (including if the requested width is zero or negative), the requested width parameter will be ignored. The maximum width specifiable is 256.

Examples

Example Result Notes
toString(50); "50"
toString(28-50); "-22"
toString(65,16); "41" decimal 65 is hex 0x41
"0x" + toString(65,0,16); "0x41" width is ignored
toString(65,4,16); "0041"
toString(12,0,2); "1100" width is ignored
toString(12,2,2); "1100" string is bigger than the requested width
toString(12,8,2); "00001100"
toString(65,1,10); "65" string is bigger than the requested width
toString(65,0,3); "65" the radix is invalid and defaults to 10
toString(65,-3,10); "65" negative widths are ignored
toString(-6552,6,10); "-06552" 6 wide includes the "-" sign