Rounding
| Function | Signature | Description |
|---|---|---|
floor | (x: f64) => f64 | Round down |
ceil | (x: f64) => f64 | Round up |
trunc | (x: f64) => f64 | Truncate toward zero |
round | (x: f64) => f64 | Round to nearest integer |
roundTo | (x: f64, decimals: i32) => f64 | Round to N decimal places |
floor()
Rounds a number down to the nearest integer.
Signature
floor(x: f64): f64tyrus
const value: f64 = 4.8;
const result: f64 = floor(value);
print(result); // 4.0ceil()
Rounds a number up to the nearest integer.
Signature
ceil(x: f64): f64tyrus
const value: f64 = 4.2;
const result: f64 = ceil(value);
print(result); // 5.0trunc()
Truncates a number toward zero, removing the fractional part.
Signature
trunc(x: f64): f64tyrus
const value: f64 = 4.7;
const result: f64 = trunc(value);
print(result); // 4.0tyrus
const value: f64 = -4.7;
const result: f64 = trunc(value);
print(result); // -4.0round()
Rounds a number to the nearest integer.
Signature
round(x: f64): f64tyrus
const value: f64 = 4.5;
const result: f64 = round(value);
print(result); // 5.0roundTo()
Rounds a number to a specified number of decimal places.
Signature
roundTo(x: f64, decimals: i32): f64tyrus
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