To boolean: Difference between revisions

From Kolmafia
Jump to navigation Jump to search
imported>Eliteofdelete
mNo edit summary
imported>Eliteofdelete
No edit summary
Line 19: Line 19:


* 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.
Note: to_boolean does not actually work on "anything".|


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}}
see_also={{SeeAlso|to_int}}

Revision as of 17:08, 21 January 2015

Function Syntax

boolean to_boolean( any anything)

  • anything is any basic data type that KoLmafia uses

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: to_boolean does not actually work on "anything".

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()