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.235Reference
function round(precision: number, value: number): numberParameters
| Parameter | Type | Description |
|---|---|---|
precision | number | The 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. |
value | number | The value to round. |
Returns
Returns the rounded value.
Throws
Error- Throws ifprecisionis 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.2345Example: Zero precision (round to integer)
import { round } from '@accelint/math';
const result = round(0, 1.2345);
// 1Example: 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); // 10000Good 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).
Related
- clamp - Clamps a number within specified bounds