To boolean: Difference between revisions

From Kolmafia
Jump to navigation Jump to search
imported>Zarqon
Added a good bit more information. Entirely removed RFI. Recommend See also: to_int(), but don't kow how to add.
imported>Eliteofdelete
Updated
 
(8 intermediate revisions by 4 users not shown)
Line 5: Line 5:
FunctionPage|
FunctionPage|
name={{#var:name}}|
name={{#var:name}}|
function_category=Datatype Conversions|


function1={{Function|
function1={{Function|
Line 12: Line 11:
return_type={{#var:return_type}}|
return_type={{#var:return_type}}|
return_also={{#var:return_also}}|
return_also={{#var:return_also}}|
parameter1= '''any''' {{pspan|anything}}|
parameter1={{Param|int|convert}}|
p1desc={{Pspan|anything}} is any basic data type that KoLmafia uses|
}}|
 
function2={{Function|
name={{#var:name}}|
aggregate={{#var:aggregate}}|
return_type={{#var:return_type}}|
return_also={{#var:return_also}}|
parameter1={{Param|float|convert}}|
}}|
 
function3={{Function|
name={{#var:name}}|
aggregate={{#var:aggregate}}|
return_type={{#var:return_type}}|
return_also={{#var:return_also}}|
parameter1={{Param|string|convert}}|
p1desc={{Pspan|convert}} is the int, float, or string to convert|
}}|
}}|


function_description=Datatype behaviour:
function_description=Datatype behaviour:
This function converts the supplied datatype to a boolean value.  For strings, this means that it will return false for everything except "true". This is case-sensitive -- "TRUE" will still return false.  For all other datatypes, it returns true unless to_int(anything) == 0.  More details:
This function converts the supplied datatype to a boolean value.  For strings, this means that it will return false for everything except "true". For all other datatypes, it returns to_int(anything) != 0.  More details:
<p>
 
* As indicated, integers return true for anything except 0, even negative values.
* As indicated, integers return true for anything except 0, even negative values.
* floats return true for anything except -1.0 to 1.0 (not inclusive).  This is because to_int(float) truncates the float.
* floats return true for anything except -1.0 to 1.0 (not inclusive).  This is because to_int(float) truncates the float.
* locations and monsters all return false.  For these datatypes, to_int() always returns 0.
Note: The string is case insensitive.|
* elements, slots, and classes return true for everything except the value that converts to 0.  This is [none] for elements, [hat] for slots, and [seal clubber] for classes.
* Other mafia datatypes (effects, familiars, items, stats and skills) all return true.  Note that for these datatypes, to_int([none]) returns -1.|


needscode=yes|
code1={{CodeSample|
title=Code Samples|
description=Following example shows basic to_boolean operations.|
code=
<syntaxhighlight>
string first, second, third, fourth;
int fifth, sixth;


first = "random stuff";
second = "true";
third = "tRuE";
fourth = "TRUE";
fifth = 5;
sixth = 0;
print(first+" gives "+to_boolean(first)+".", "blue");
print(second+" gives "+to_boolean(second)+".", "blue");
print(third+" gives "+to_boolean(third)+".", "blue");
print(fourth+" gives "+to_boolean(fourth)+".", "blue");
print(fifth+" gives "+to_boolean(fifth)+".", "blue");
print(sixth+" gives "+to_boolean(sixth)+".", "blue");
</syntaxhighlight>|
moreinfo=It gives the following output.
<pre>
random stuff gives false.
true gives true.
tRuE gives true.
TRUE gives true.
5 gives true.
0 gives false.
</pre>
}}|
see_also={{SeeAlso|to_int}}
}}
}}
[[Category:Datatype Conversions]]

Latest revision as of 14:18, 26 January 2015

Function Syntax

boolean to_boolean(int convert )

boolean to_boolean(float convert )

boolean to_boolean(string convert )

  • convert is the int, float, or string to convert

Datatype behaviour: This function converts the supplied datatype to a boolean value. For strings, this means that it will return false for everything except "true". For all other datatypes, it returns to_int(anything) != 0. More details:

  • As indicated, integers return true for anything except 0, even negative values.
  • floats return true for anything except -1.0 to 1.0 (not inclusive). This is because to_int(float) truncates the float.

Note: The string is case insensitive.

Code Samples

Following example shows basic to_boolean operations.

string first, second, third, fourth;
int fifth, sixth;

first = "random stuff";
second = "true";
third = "tRuE";
fourth = "TRUE";
fifth = 5;
sixth = 0;

print(first+" gives "+to_boolean(first)+".", "blue");
print(second+" gives "+to_boolean(second)+".", "blue");
print(third+" gives "+to_boolean(third)+".", "blue");
print(fourth+" gives "+to_boolean(fourth)+".", "blue");
print(fifth+" gives "+to_boolean(fifth)+".", "blue");
print(sixth+" gives "+to_boolean(sixth)+".", "blue");

It gives the following output.

random stuff gives false.
true gives true.
tRuE gives true.
TRUE gives true.
5 gives true.
0 gives false.

See Also

to_int()