The q* stdlib docs
qconvert: type casting
qconvert provides the convert.* built-ins: cross-type casts from one Value to another. It is exact where it can be — to_decimal uses qvalue's BigDecimal — and lenient where it must be, with well-defined defaults so a cast never fails or panics. Each function takes a single value at positional key "0" and returns the cast value under result.
What it gives you#
qconvert registers five public built-ins, all single-argument casts following the shared DMS convention.
| Function | Returns | Behavior |
|---|---|---|
| convert.to_str | Str | Textual form of the value. |
| convert.to_int | Int | Coerce to f64 then truncate toward zero. |
| convert.to_float | Float | Coerce to f64. |
| convert.to_bool | Bool | Truthiness of the value. |
| convert.to_decimal | Decimal | Exact BigDecimal when the value is/looks numeric, else 0. |
to_str rules#
Str is returned unchanged. Int, Timestamp, Float, Bool, BigInt, and Decimal use their natural textual form. Null becomes the empty string. Any other type falls back to its debug representation.
to_int and to_float rules#
Both build an f64 first. Int and Timestamp cast directly; Float is used as-is; Bool becomes 1.0 or 0.0; BigInt and Decimal are stringified then parsed (0.0 on failure); a Str is trimmed and parsed (0.0 if not a number); any other type is 0.0. to_float returns that f64. to_int truncates it toward zero (so 3.9 becomes 3, and -3.9 becomes -3) and returns an Int.
to_bool rules#
Truthiness is defined per type: a Bool is itself; an Int is true when non-zero; a Float is true when non-zero; a Str is true only when it trims and lowercases to one of "true", "1", "yes", or "on"; Null is false; any other type is true.
- "yes", "on", "1", "true"
- These strings (case-insensitive, trimmed) convert to true.
- any other string
- Converts to false (including "", "no", "0", "false").
to_decimal rules#
to_decimal is exact where possible. If the value already is or looks like a number, it produces the exact BigDecimal: it tries the value's own BigDecimal conversion first, and otherwise stringifies the value and parses that text as a decimal. If neither yields a number, it returns the Decimal 0.
Resource budget#
A single value in, a single cast out; to_str builds a String and to_decimal parses/allocates a BigDecimal, both proportional to the value. Each convert.* function declares a 1024-byte (1 KB) memory budget and zero storage.
Examples#
to_int parses a string then truncates; to_bool recognizes "yes" as truthy.
Request
convert.to_int("42")
convert.to_int(3.9)
# wire form for the first:
{ "0": { "Str": "42" } }Response
{ "result": { "Int": 42 } }
{ "result": { "Int": 3 } }Output
42
3Request
convert.to_bool("yes")
convert.to_bool(0)
convert.to_str(7)
convert.to_float("1.5")Response
{ "result": { "Bool": true } }
{ "result": { "Bool": false } }
{ "result": { "Str": "7" } }
{ "result": { "Float": 1.5 } }Output
true
false
7
1.5