function round
round(
x: number,
n?: number,
): number

Round to nearest integer.

Rounds each element in x to the nearest integer or specified number of decimal places. Handles numbers, arrays, and matrices element-wise.

Examples

Round a number to 12 decimal places

import { assertEquals } from "jsr:@std/assert";

assertEquals(round(Math.PI, 12), 3.14159265359);

Round an array of numbers to 2 decimal places

import { assertEquals } from "jsr:@std/assert";

assertEquals(round([-1.4543, 4.5234], 2), [-1.45, 4.52]);

Round an array of numbers to the nearest integer

import { assertEquals } from "jsr:@std/assert";

assertEquals(round([-1.9, -0.2, 3.4, 5.6, 7.0]), [-2, 0, 3, 6, 7]);

Round a matrix of numbers to the nearest integer

import { assertEquals } from "jsr:@std/assert";

assertEquals(round([[1.45, -2.3], [1.1, -4.3]]), [[1, -2], [1, -4]]);

Round a matrix of numbers to 1 decimal place

import { assertEquals } from "jsr:@std/assert";

assertEquals(round([[1.456, -2.354], [1.123, -4.345]], 1), [[1.5, -2.4], [1.1, -4.3]]);

Round a single number without specifying decimal places

import { assertEquals } from "jsr:@std/assert";

assertEquals(round(5.678), 6);

Round an array of negative numbers to 2 decimal places

import { assertEquals } from "jsr:@std/assert";

assertEquals(round([-1.4567, -4.5234], 2), [-1.46, -4.52]);

Round a single negative number to 2 decimal places

import { assertEquals } from "jsr:@std/assert";

assertEquals(round(-2.34567, 2), -2.35);

Parameters

x: number

The value(s) to be rounded

optional
n: number

The number of decimal places to round to (default is 0)

Return Type

number

The rounded value(s)

Throws

If no arguments are provided

round(
x: array,
n?: number,
): array

Round to nearest integer.

Rounds each element in x to the nearest integer or specified number of decimal places. Handles numbers, arrays, and matrices element-wise.

Examples

Round a number to 12 decimal places

import { assertEquals } from "jsr:@std/assert";

assertEquals(round(Math.PI, 12), 3.14159265359);

Round an array of numbers to 2 decimal places

import { assertEquals } from "jsr:@std/assert";

assertEquals(round([-1.4543, 4.5234], 2), [-1.45, 4.52]);

Round an array of numbers to the nearest integer

import { assertEquals } from "jsr:@std/assert";

assertEquals(round([-1.9, -0.2, 3.4, 5.6, 7.0]), [-2, 0, 3, 6, 7]);

Round a matrix of numbers to the nearest integer

import { assertEquals } from "jsr:@std/assert";

assertEquals(round([[1.45, -2.3], [1.1, -4.3]]), [[1, -2], [1, -4]]);

Round a matrix of numbers to 1 decimal place

import { assertEquals } from "jsr:@std/assert";

assertEquals(round([[1.456, -2.354], [1.123, -4.345]], 1), [[1.5, -2.4], [1.1, -4.3]]);

Round a single number without specifying decimal places

import { assertEquals } from "jsr:@std/assert";

assertEquals(round(5.678), 6);

Round an array of negative numbers to 2 decimal places

import { assertEquals } from "jsr:@std/assert";

assertEquals(round([-1.4567, -4.5234], 2), [-1.46, -4.52]);

Round a single negative number to 2 decimal places

import { assertEquals } from "jsr:@std/assert";

assertEquals(round(-2.34567, 2), -2.35);

Parameters

The value(s) to be rounded

optional
n: number

The number of decimal places to round to (default is 0)

Return Type

The rounded value(s)

Throws

If no arguments are provided

round(
x: matrix,
n?: number,
): matrix

Round to nearest integer.

Rounds each element in x to the nearest integer or specified number of decimal places. Handles numbers, arrays, and matrices element-wise.

Examples

Round a number to 12 decimal places

import { assertEquals } from "jsr:@std/assert";

assertEquals(round(Math.PI, 12), 3.14159265359);

Round an array of numbers to 2 decimal places

import { assertEquals } from "jsr:@std/assert";

assertEquals(round([-1.4543, 4.5234], 2), [-1.45, 4.52]);

Round an array of numbers to the nearest integer

import { assertEquals } from "jsr:@std/assert";

assertEquals(round([-1.9, -0.2, 3.4, 5.6, 7.0]), [-2, 0, 3, 6, 7]);

Round a matrix of numbers to the nearest integer

import { assertEquals } from "jsr:@std/assert";

assertEquals(round([[1.45, -2.3], [1.1, -4.3]]), [[1, -2], [1, -4]]);

Round a matrix of numbers to 1 decimal place

import { assertEquals } from "jsr:@std/assert";

assertEquals(round([[1.456, -2.354], [1.123, -4.345]], 1), [[1.5, -2.4], [1.1, -4.3]]);

Round a single number without specifying decimal places

import { assertEquals } from "jsr:@std/assert";

assertEquals(round(5.678), 6);

Round an array of negative numbers to 2 decimal places

import { assertEquals } from "jsr:@std/assert";

assertEquals(round([-1.4567, -4.5234], 2), [-1.46, -4.52]);

Round a single negative number to 2 decimal places

import { assertEquals } from "jsr:@std/assert";

assertEquals(round(-2.34567, 2), -2.35);

Parameters

The value(s) to be rounded

optional
n: number

The number of decimal places to round to (default is 0)

Return Type

The rounded value(s)

Throws

If no arguments are provided