For AI agents: the complete documentation index is available at /llms.txt, the full documentation bundle is available at /llms-full.txt, and this page is available as Markdown at /reference/math/round.md.

round es-toolkit

Rounds a number to a specified number of decimal places.

const rounded = round(value, precision?);

Usage

round(value, precision?)

Use round when you want to round a number to a specific number of decimal places. It's a mathematical function for precise calculations.

import { round } from 'es-toolkit/math';

// Default - rounds to integer.
const num1 = round(1.2345);
console.log(num1); // 1

// Round to 2 decimal places.
const num2 = round(1.2345, 2);
console.log(num2); // 1.23

// Round to 3 decimal places.
const num3 = round(1.2387, 3);
console.log(num3); // 1.239

// Can also round negative numbers.
const num4 = round(-1.2345, 2);
console.log(num4); // -1.23

// Handles large numbers too.
const num5 = round(123.456789, 4);
console.log(num5); // 123.4568

Useful in price calculations and statistics.

import { round } from 'es-toolkit/math';

// Price calculation (to 2 decimal places)
const price = 19.999;
const finalPrice = round(price, 2);
console.log(finalPrice); // 20.00

// Percentage calculation (to 1 decimal place)
const percentage = 66.66666;
const displayPercentage = round(percentage, 1);
console.log(displayPercentage); // 66.7

// Rating calculation (to 1 decimal place)
const rating = 4.267;
const displayRating = round(rating, 1);
console.log(displayRating); // 4.3

Round in calculations where accuracy matters.

import { round } from 'es-toolkit/math';

// Clean up math calculation results
const result = Math.PI * 2;
const cleanResult = round(result, 5);
console.log(cleanResult); // 6.28319

// Round measurement values
const measurement = 15.789123;
const rounded = round(measurement, 3);
console.log(rounded); // 15.789

Invalid precision values throw an error.

import { round } from 'es-toolkit/math';

// Error occurs if precision is not an integer.
try {
  round(1.23, 2.5);
} catch (error) {
  console.error(error.message); // 'Precision must be an integer.'
}

Parameters

valuenumberrequired

The number to round.

precisionnumberoptional

The number of decimal places. Must be an integer. Defaults to 0.

Returns

Returns the number rounded to the specified precision.

number

Throws

  • Throws an error if precision is not an integer.
Source: es-toolkit

Re-exported verbatim from es-toolkit. Implementation, edge cases, and performance behavior are owned upstream. This page mirrors the documentation at the pinned version; the linked source is authoritative.