Version: v0.8.1 - Beta.  We welcome contributors & feedback.

Number.round

round($numDecDigits=0, $options={}) -> number

Description

Round to the specified number of digits after the decimal point.

By default, half fractions are rounded up. (e.g. 1.5 = 2, -1.5 = -2)

$num = 3.2
$num.round()  //= 3

$num = 3.5
$num.round()  //= 4

$num = 3.8
$num.round()  //= 4

$num = 1.2538
$num.round(2)  //= 1.25

$num = 1.025
$num.round(2)  //= 1.03

If $numDecimalDigits is negative, it rounds to digits left of the decimal point.

$num = 1025
$num.round(-1)  //= 1030

Half Option

The half option sets what direction the number is rounded to when it is at exactly half (e.g. 0.5).

Option Description
up Round up. (toward positive infinity) (default)
down Round down. (toward negative infinity)
even Round to the nearest even number.
odd Round to the nearest odd number.

$num = 3.5
$num.round(0, { half: 'up' })    //= 4
$num.round(0, { half: 'down' })  //= 3
$num.round(0, { half: 'odd' })   //= 3
$num.round(0, { half: 'even' })  //= 4

$num = -3.5
$num.round(0, { half: 'up' })    //= -3
$num.round(0, { half: 'down' })  //= -4
$num.round(0, { half: 'odd' })   //= -3
$num.round(0, { half: 'even' })  //= -4

Comparison of Rounding Methods

Method Description Example
floor Round down (toward negative infinity) 4.7 → 4
ceiling Round up (toward positive infinity) 2.3 → 3
toInt Drop all digits after the decimal point 7.33 → 7
round Round to a given precision 6.56 → 6.5

See Also