es-toolkit/docs/reference/math/round.md
배성준 92c9db00ba
fix(round): Throw an exception when precision is not an integer (#79)
* feat: add exception when precision is integer

* test: add testcase for precision exception

* docs: add examples for exception
2024-06-20 11:05:33 +09:00

744 B

round

Rounds a number to a specified precision.

This function takes a number and an optional precision value, and returns the number rounded to the specified number of decimal places.

Signature

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

Parameters

  • value (number): The number to round.
  • precision (number, optional): The number of decimal places to round to. Defaults to 0.

Returns

(number): The rounded number.

Examples

const result1 = round(1.2345); // result1 will be 1
const result2 = round(1.2345, 2); // result2 will be 1.23
const result3 = round(1.2345, 3); // result3 will be 1.235
const result4 = round(1.2345, 3.1); // This will throw an error