1function roundIt(n, d = 0) {
2 var m = Math.pow(10, d);
3 var n = +(d ? n * m : n).toFixed(8);
4 var i = Math.floor(n),
5 diff = n - i; // getting the difference
6 var e = 1e-8; // Rounding errors in var(diff)
7 // Checking if the difference is less than or
8 // greater than, based on that adding the 1 to it.
9 var r = (diff > 0.5 - e && diff < 0.5 + e) ?
10 ((i % 2 == 0) ? i : i + 1) : Math.round(n);
11 return d ? r / m : r; // if d != 0 then returning r/m else r
12}