Difference between revisions of "SHIP:Sail:toString"

From Serious Documentation
Jump to: navigation, search
(Examples)
(Examples)
Line 62: Line 62:
 
results in the string "00001100"
 
results in the string "00001100"
  
"<code>tostring(65,10,1);</code>"
+
<code>tostring(65,10,1); </code>
 
results in the string "65" (the string is bigger than the requested width)
 
results in the string "65" (the string is bigger than the requested width)
  
"<code>tostring(65,3);</code>"
+
<code>tostring(65,3); </code>
 
results in the string "65" (the radix is invalid and defaults to 10)
 
results in the string "65" (the radix is invalid and defaults to 10)

Revision as of 05:58, 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

tostring(50); results in the string "50".

tostring(50-28); results in the string "-22".

tostring(65,16); results in the string "41", which is hex 0x41, which is decimal 65.

"0x" + tostring(65,16); results in the string "0x41".

tostring(65,16,4); results in the string "0041".

tostring(12,2); results in the string "1100".

tostring(12,2,2); results in the string "1100" (the string is bigger than the requested width)

tostring(12,2,8); results in the string "00001100"

tostring(65,10,1); results in the string "65" (the string is bigger than the requested width)

tostring(65,3); results in the string "65" (the radix is invalid and defaults to 10)