round - nodef/extra-number GitHub Wiki

Round a number to specific precision.

Similar: floor, ceil, round.

function round(x, pre)
// x:   a number
// pre: to precision [1]
const xnumber = require('extra-number');


xnumber.round(9.135, 1);
// → 9

xnumber.round(9.135, 1e-2);
// → 9.14

xnumber.round(9.1357, 0.05);
// → 9.15

0.1 + 0.2
// → 0.30000000000000004 (why?)

xnumber.round(0.1 + 0.2, 1e-12);
// → 0.3 (nice!)

References