Skip to content

Rounding ​

FunctionSignatureDescription
floor(x: f64) => f64Round down
ceil(x: f64) => f64Round up
trunc(x: f64) => f64Truncate toward zero
round(x: f64) => f64Round to nearest integer
roundTo(x: f64, decimals: i32) => f64Round to N decimal places

floor() ​

Rounds a number down to the nearest integer.

Signaturefloor(x: f64): f64
tyrus
const value: f64 = 4.8;
const result: f64 = floor(value);
print(result); // 4.0

ceil() ​

Rounds a number up to the nearest integer.

Signatureceil(x: f64): f64
tyrus
const value: f64 = 4.2;
const result: f64 = ceil(value);
print(result); // 5.0

trunc() ​

Truncates a number toward zero, removing the fractional part.

Signaturetrunc(x: f64): f64
tyrus
const value: f64 = 4.7;
const result: f64 = trunc(value);
print(result); // 4.0
tyrus
const value: f64 = -4.7;
const result: f64 = trunc(value);
print(result); // -4.0

round() ​

Rounds a number to the nearest integer.

Signatureround(x: f64): f64
tyrus
const value: f64 = 4.5;
const result: f64 = round(value);
print(result); // 5.0

roundTo() ​

Rounds a number to a specified number of decimal places.

SignatureroundTo(x: f64, decimals: i32): f64
tyrus
const value: f64 = 3.14159;
const result1: f64 = roundTo(value, 2);
const result2: f64 = roundTo(value, 4);
print(result1); // 3.14
print(result2); // 3.1416