Difference between revisions of "SHIP:Sail:toString"

From Serious Documentation
Jump to: navigation, search
(Prototype)
(Prototype)
Line 8: Line 8:
  
 
<code>{{DataType|Integer}} toString({{DataType|Integer}} number);</code>
 
<code>{{DataType|Integer}} toString({{DataType|Integer}} number);</code>
 +
 
<code>{{DataType|Integer}} toString({{DataType|Integer}} number, {{DataType|Integer}} radix);</code>
 
<code>{{DataType|Integer}} toString({{DataType|Integer}} number, {{DataType|Integer}} radix);</code>
 +
 
<code>{{DataType|Integer}} toString({{DataType|Integer}} number, {{DataType|Integer}} radix, {{DataType|Integer}} width);</code>
 
<code>{{DataType|Integer}} toString({{DataType|Integer}} number, {{DataType|Integer}} radix, {{DataType|Integer}} width);</code>
  

Revision as of 10:42, 5 December 2012

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, Integer radix);

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

Parameters/Return Value

Parameter Data Type Description
number Number The number (or any expression returning number) to convert
radix Number The radix of the string (10 is decimal, 16 is hex, 2 is binary, etc.)
width Number The desired width of the number, including any "-" sign, left padded with "0"
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.

The maximum width specifiable is 256. Negative widths are ignored. Widths smaller than the string would normally result in are also ignored.

Examples

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