Standard Toolkit
Packages@accelint/math

round

Rounds a number to a specified precision

Usage

import { round } from '@accelint/math';

const result = round(1, 1.2345);
// 1.2

const result = round(2, 1.2345);
// 1.23

const result = round(3, 1.2345);
// 1.235

Reference

function round(precision: number, value: number): number

Parameters

ParameterTypeDescription
precisionnumberThe precision of the rounded output. Must be an integer. Positive values round to decimal places; negative values round to positions left of the decimal point.
valuenumberThe value to round.

Returns

Returns the rounded value.

Throws

  • Error - Throws if precision is not an integer.

Examples

Example: Positive precision (decimal places)

import { round } from '@accelint/math';

const value = 1.2345;

round(1, value); // 1.2
round(2, value); // 1.23
round(3, value); // 1.235
round(4, value); // 1.2345

Example: Zero precision (round to integer)

import { round } from '@accelint/math';

const result = round(0, 1.2345);
// 1

Example: Negative precision (round left of decimal)

import { round } from '@accelint/math';

const value = 12345.6789;

round(-1, value); // 12350
round(-2, value); // 12300
round(-3, value); // 12000
round(-4, value); // 10000

Good to know: Precision must be an integer. Positive values round to decimal places (e.g., 1 = tenths, 2 = hundredths). Negative values round to positions left of the decimal point (e.g., -1 = tens, -2 = hundreds).

  • clamp - Clamps a number within specified bounds

On this page