Difference between revisions of "SHIP:Sail:toString"

From Serious Documentation
Jump to: navigation, search
(Prototype)
(Examples)
Line 52: Line 52:
 
|<code>toString(65,16);</code> || "41" || decimal 65 is hex 0x41
 
|<code>toString(65,16);</code> || "41" || decimal 65 is hex 0x41
 
|-
 
|-
|<code>"0x" + toString(65,16);</code> || "0x41"||
+
|<code>"0x" + toString(65,0,16);</code> || "0x41"|| width is ignored
 
|-
 
|-
|<code>toString(65,16,4);</code> || "0041" ||
+
|<code>toString(65,4,16);</code> || "0041" ||
 
|-
 
|-
|<code>toString(12,2);</code> || "1100" ||
+
|<code>toString(12,0,2);</code> || "1100" || width is ignored
 
|-
 
|-
 
|<code>toString(12,2,2);</code> || "1100" || string is bigger than the requested width
 
|<code>toString(12,2,2);</code> || "1100" || string is bigger than the requested width
 
|-
 
|-
|<code>toString(12,2,8);</code> || "00001100" ||
+
|<code>toString(12,8,2);</code> || "00001100" ||
 
|-
 
|-
|<code>toString(65,10,1); </code> || "65" ||  string is bigger than the requested width
+
|<code>toString(65,1,10); </code> || "65" ||  string is bigger than the requested width
 
|-
 
|-
|<code>toString(65,3); </code> || "65"|| the radix is invalid and defaults to 10
+
|<code>toString(65,0,3); </code> || "65"|| the radix is invalid and defaults to 10
 
|-
 
|-
|<code>toString(65,10,-3); </code> || "65"|| negative widths are ignored
+
|<code>toString(65,-3,10); </code> || "65"|| negative widths are ignored
 
|-
 
|-
|<code>toString(-6552,10,6); </code> || "-06552"|| 6 wide includes the "-" sign
+
|<code>toString(-6552,6,10); </code> || "-06552"|| 6 wide includes the "-" sign
 
|}
 
|}

Revision as of 11:53, 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 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, left padded with "0"
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.

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,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