SHIP:Sail:toString

From Serious Documentation
Revision as of 11:57, 5 December 2012 by Admin (talk | contribs)
Jump to: navigation, search

SHIP Sail Reference 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, Short width);

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

Parameters/Return Value

Parameter Data Type Description
number Integer The number (or any expression returning number) to convert
width Short The desired width of the number, including any "-" sign, left padded with "0"
radix Byte 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