Skip to content

02 · Primitives & Numbers

Integers & Floats

int and float, arithmetic, and int → float coercion.

On this page

int and float are the two core numeric types. Integer division promotes to float; the reverse needs an explicit call via std::math. Float literals accept scientific notation (1.5e3):

Decimal literals, the arithmetic operators, and int → float coercion.

02-numbers-int-float.zolo
Playground
// Feature: Numbers — `int` and `float`
// Syntax: decimal literals; `.` introduces a float
// When to use: arithmetic. `int` and `float` are distinct static types;
// use an explicit cast when a mixed operation should produce a float.

use std::math

// Integers — no decimal point.
let a: int = 42
let b: int = -17

// Floats — with a dot OR scientific notation.
let c: float = 3.14
let d: float = 1.5e3  // 1500.0
let e: float = 2.5e-2  // 0.025

// Operations: + - * / % ** (power)
print(a + b)  // 25
print(c * 2.0)  // 6.28
print(a / 5)  // 8.4 (division promotes to float)
print(a % 5)  // 2
print(2 ** 10)  // 1024

// Mixed arithmetic uses an explicit conversion.
let f = (a as float) + c  // f: float = 45.14
print(f)

// Explicit conversion via stdlib.
let g = math.floor(3.9)  // 3
let h = math.round(3.5)  // 4
print(g, h)

See also

Global index

Find your way through Zolo

Try an idea

Start here

9 results

9 results

en