Difference between revisions of "SHIP:Sail:toString"

From Serious Documentation
Jump to: navigation, search
m (Admin moved page SHIP:Sail:tostring to SHIP:Sail:toString without leaving a redirect)
Line 1: Line 1:
 
[[SHIP:Sail|SHIP Sail Reference Home]]
 
[[SHIP:Sail|SHIP Sail Reference Home]]
 
__NOTOC__
 
__NOTOC__
== tostring ==
+
== toString ==
  
 
Converts a number to a string, with optional radix and optional leading 0 created width.
 
Converts a number to a string, with optional radix and optional leading 0 created width.
Line 7: Line 7:
 
== Prototype ==
 
== Prototype ==
  
<code>s = tostring(number);</code>
+
<code>s = toString(number);</code>
<code>s = tostring(number, radix);</code>
+
<code>s = toString(number, radix);</code>
<code>s = tostring(number, radix, width);</code>
+
<code>s = toString(number, radix, width);</code>
  
 
=== Parameters/Return Value ===
 
=== Parameters/Return Value ===
Line 30: Line 30:
 
== Detailed Description ==
 
== Detailed Description ==
  
The <code>tostring(number)</code> function returns the string equivalent of a number.  Optional <code>radix</code> and <code>width</code> parameters allow conversion into a different base (for instance, base 16 hexadecimal) and left-zero padded to a specific width.  
+
The <code>toString(number)</code> function returns the string equivalent of a number.  Optional <code>radix</code> and <code>width</code> 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.
 
Supported radixes are 2,8,10, and 16.  Using a non-supported radix reverts to base 10.
Line 44: Line 44:
 
! scope="col" style="text-align:left" | Notes
 
! scope="col" style="text-align:left" | Notes
 
|-
 
|-
|<code>tostring(50);</code> || "50" ||
+
|<code>toString(50);</code> || "50" ||
 
|-
 
|-
|<code>tostring(50-28);</code> || "-22" ||
+
|<code>toString(50-28);</code> || "-22" ||
 
|-
 
|-
|<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,16);</code> || "0x41"||
 
|-
 
|-
|<code>tostring(65,16,4);</code> || "0041" ||
+
|<code>toString(65,16,4);</code> || "0041" ||
 
|-
 
|-
|<code>tostring(12,2);</code> || "1100" ||
+
|<code>toString(12,2);</code> || "1100" ||
 
|-
 
|-
|<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,2,8);</code> || "00001100" ||
 
|-
 
|-
|<code>tostring(65,10,1); </code> || "65" ||  string is bigger than the requested width
+
|<code>toString(65,10,1); </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,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,10,-3); </code> || "65"|| negative widths are ignored
 
|-
 
|-
|<code>tostring(-6552,10,6); </code> || "-06552"|| 6 wide includes the "-" sign
+
|<code>toString(-6552,10,6); </code> || "-06552"|| 6 wide includes the "-" sign
 
|}
 
|}

Revision as of 06:08, 5 December 2012

SHIP Sail Reference Home

toString

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

Prototype

s = toString(number); s = toString(number, radix); s = toString(number, radix, 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. If th

Examples

Example Result Notes
toString(50); "50"
toString(50-28); "-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