How to make round half even in javascript unlimited number after dot?

I want to have 26.955 rounded to 26.96

I mean if I have numbers 0-4 I want to round them and if they are 5-9 I want to round them.

I counted parseFloat(number).toFixed(2)

, but it returns me 26.95, I need 26.96 I used Math.round

, but that doesn't work either, I saw Math.round10

, but he said that this function does not exist, so I donโ€™t know how to solve my problem.

UPDATE: I don't always have 3 digits after the dot I have more than I would have 26.956736489

your mentioned duplicate conversations about .fixed (2) I say it doesn't work half way even , it is not a duplicate

+3


source to share


2 answers


Try

Math.round(29.955*100)/100; //29.96

      



Or a cool functional approach

function round(n) {
   n = Math.pow(10,n);
   return function(num) {
       return Math.round(num*n)/n;
   }
}

var round2 = round(2);
var num = 29.9555;
console.log(round2(num));

      

+3


source


use this:



var num=26.955
Math.round(num* 100) / 100;

      

0


source







All Articles