Skip to content

round

Rounds a number to a specified precision.

219 bytes
since v12.2.0

Usage

The _.round function rounds a given number to a specified precision.

import * as _ from 'radashi'
_.round(123.456) // => 123
_.round(1234.56, -2) // => 1200

Precision

The precision argument is limited to be within the range of -323 to +292. Without this limit, precision values outside this range can result in NaN.

With nonzero precision, round may return positive zero when the rounding method returns negative zero. Use Object.is to distinguish the two:

Object.is(_.round(-0.04, 1), 0) // => true
Object.is(_.round(-0.04, 1), -0) // => false

Rounding Method

You may provide a custom rounding method. The default is Math.round.

_.round(4.001, 2, Math.ceil) // => 4.01
_.round(4.089, 2, Math.floor) // => 4.08