tobool
This page explains how to use the tobool function in APL.
Use the tobool function to convert various data types to a boolean value. This is helpful when you need to normalize values from different sources into boolean format for conditional logic, filtering, or boolean operations.
You typically use tobool when working with data that represents boolean values as strings (like "true"/"false" or "1"/"0"), numbers, or other types that need to be converted to proper boolean values.
Usage
Syntax
Parameters
| Name | Type | Description |
|---|---|---|
| value | dynamic | The value to convert to boolean. |
Returns
If conversion is successful, the result is a boolean. If conversion isn’t successful, the result is false.
Conversion behavior
The tobool function converts values based on their type:
- Boolean: Returns the value unchanged.
- Integer/Float/Duration: Returns
trueif the value isn't equal to 0, otherwisefalse. - Datetime: Returns
trueif the value is anything other than epoch (zero time), otherwisefalse. - String: Returns
trueif the value equals"1","t","T","TRUE","true", or"True". Returnsfalseif the value equals"0","f","F","FALSE","false", or"False". Returnsnullfor any other string value.
Example
Convert string representations of Boolean values to actual Boolean types for filtering and analysis.
Query
Output
| _time | uri | status | is_active | enabled | is_active | is_enabled |
|---|---|---|---|---|---|---|
| Jun 24, 09:28:10 | /api/users | 200 | "true" | "1" | true | true |
This example converts string representations of boolean values (like "true" and "1") to actual boolean types, enabling proper boolean logic and filtering.
List of related functions
- isbool: Checks if a value is a boolean. Use
isboolto validate types, andtoboolto convert values to boolean. - case: Evaluates conditions and returns values. Use
casefor complex conditional logic, andtoboolfor simple conversions. - iff: Returns one of two values based on a predicate. Use
ifffor conditional assignment, andtoboolfor type conversion.