Reference · Vectors

vec.round

signature
vec.round(list, int=) -> list

What it does

Each item rounded. Without digits the result is a list of ints, rounded to the nearest whole number with halves away from zero. With digits the result is a list of floats rounded to that many decimal places, as round(x, digits) does.

Examples

print(vec.round([1.4, 1.5, 2.5, -1.5]))print(vec.round([1.2345, 2.5], 2))
output
[1, 2, 3, -2][1.23, 2.5]

Edge cases

print(vec.round([1, 2]), vec.round([1, 2], 1), vec.round([1.5], 0))print(vec.round([1234.5], -1))print(vec.round([0.1 + 0.2], 20))print(vec.round([math.inf, float("nan")]))print(vec.round([]), vec.round(null))
output
[1, 2] [1.0, 2.0] [2.0][1235.0][0.3][inf, NaN][] []

Errors

An item that is not an int or a float, a digits that is not an int, or a first argument that is not a list are runtime errors, not error values.

this one fails
print(vec.round([1.5], "2"))
output
line 1: `vec.round` expects an int as argument 2, got str

Chaining

vec.round usually ends a pipeline, after the arithmetic:

let sales = [3, 5, 4, 6, 8]print(sales | vec.div(sum(sales)) | vec.mul(100) | vec.round(1))print(vec.linspace(0, 1, 5) | vec.mul(100) | vec.round)
output
[11.5, 19.2, 15.4, 23.1, 30.8][0, 25, 50, 75, 100]

See also

round, vec.clip, vec.div, int

Thread
7Useful? Like it, or add to its thread below.

Thread 0 posts

On vec.round(): questions, tips, corrections, and code that uses it.

No posts yet. The first one opens the thread.

    Edit this page on GitHub · every example's output was made by running it (tools/refcheck.mjs) · questions on Discord or r/quillo