Difference between revisions of "To string"

From Kolmafia
Jump to navigation Jump to search
imported>StDoodle
(Created page with '{{ #vardefine:name|to_string}}{{ #vardefine:return_type|string}}{{ FunctionPage| name={{#var:name}}| function_category=Datatype Conversions| function1={{Function| name={{#var:n…')
 
imported>Deadned
m
 
(4 intermediate revisions by 3 users not shown)
Line 5: Line 5:
 
FunctionPage|
 
FunctionPage|
 
name={{#var:name}}|
 
name={{#var:name}}|
function_category=Datatype Conversions|
 
  
 
function1={{Function|
 
function1={{Function|
Line 16: Line 15:
 
}}|
 
}}|
  
function_description=Returns any datatype as its string equivalent.|
+
function2={{Function|
 +
name={{#var:name}}|
 +
aggregate={{#var:aggregate}}|
 +
return_type={{#var:return_type}}|
 +
return_also={{#var:return_also}}|
 +
parameter1= '''num''' {{pspan|number}}|
 +
parameter2= '''string''' {{pspan|format}}|
 +
p1desc={{Pspan|number}} is either an int or a float|
 +
p2desc={{Pspan|format}} is a format string used by the [http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Formatter.html Java formatter]|
 +
}}|
  
needscode=yes|
+
function_description=Returns any datatype as its string equivalent. For int and float types, you may optionally specify a format string, which can be useful for (among other things) printing only a few decimal places, or placing commas to break up large numerals.|
 +
 
 +
code1={{CodeSample|
 +
title=Code Sample|
 +
description=Displays only two decimal places|
 +
code=
 +
<syntaxhighlight>
 +
to_string(123.456789,"%.2f")
 +
</syntaxhighlight>
 +
Returned: 123.46}}
 +
{{CodeSample|
 +
description=Displays an integer with commas.|
 +
code=<syntaxhighlight>
 +
to_string(10000000,"%,d")
 +
</syntaxhighlight>
 +
Returned: 10,000,000}}|
  
 
special=Note that this function isn't needed for print() and string concatenation, as KoLmafia handles conversions transparently in those cases; however, this function is still useful for passing a non-string value as a string parameter, or a part thereof.|
 
special=Note that this function isn't needed for print() and string concatenation, as KoLmafia handles conversions transparently in those cases; however, this function is still useful for passing a non-string value as a string parameter, or a part thereof.|
  
 
}}
 
}}
 +
 +
[[Category:Datatype Conversions]]

Latest revision as of 04:14, 21 April 2013

Function Syntax

string to_string( any anything)

  • anything is any data type that KoLmafia uses

string to_string( num number, string format)

  • number is either an int or a float
  • format is a format string used by the Java formatter

Returns any datatype as its string equivalent. For int and float types, you may optionally specify a format string, which can be useful for (among other things) printing only a few decimal places, or placing commas to break up large numerals.

Code Sample

Displays only two decimal places

to_string(123.456789,"%.2f")

Returned: 123.46

Displays an integer with commas.

to_string(10000000,"%,d")

Returned: 10,000,000

Special

Note that this function isn't needed for print() and string concatenation, as KoLmafia handles conversions transparently in those cases; however, this function is still useful for passing a non-string value as a string parameter, or a part thereof.