Skip to content
Subhajit Sahu edited this page Mar 1, 2020 · 6 revisions

Rounds number to specific precision.

Also helps to deal with number precision problems.

number.round(n, [pre]);
// n:   a number
// pre: to precision (1e-12)
const number = require('extra-number');

/* 1. round to specific precision */
number.round(9.135, 1);
// 9

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

number.round(9.1357, 0.05);
// 9.15

 
/* 2. round precision errors */
0.1*0.2
// 0.020000000000000004

number.round(0.1*0.2);
// 0.02

references

Clone this wiki locally