Velo by Example: Type Conversions

Velo provides built-in properties and extension functions for converting between types.

Convert integers and other types to strings with .str.

int num = 42;
str s = num.str;       # "42"

float pi = 3.14;
str fs = pi.str;       # "3.14"

Parse strings to integers with .int.

str text = "123";
int n = text.int;      # 123

The bool.vel module adds bool conversion extensions.

include "lang/bool.vel";

bool flag = true;
str s = flag.str;      # "true"
int i = flag.int;      # 1
bool neg = flag.not;   # false

The int.vel module adds formatting and math extensions.

include "lang/int.vel";

int x = -42;
int absVal = x.abs();          # 42
str hex = (255).format(16);    # "ff"
str bin = (10).format(2);      # "1010"

Convert between strings and byte arrays with str.vel and array.vel.

include "lang/str.vel";
include "lang/array.vel";

str text = "Hello";
array[byte] bytes = text.bytes();
str back = bytes.str();   # "Hello"