math - Javascript Rounding to the nearest 2 decimal (however 5 rounds down) -
All my values are coming back from the server as 3 decimal places. I have to score 10, 2 decimal places in the nearest place, in the past. Grip from decimal (18,2) decimal (18,3) is that when it is a 5, then it needs to be rounded.
I need to do this in javascript: D
I can not guarantee that 3 decimal places will return, this is the maximum. It seems that you only want special rounding, where the final digit is 5, so the exam is for him and in those cases differently Do the goals from: ex 4.494 - & gt; 4.4 9 ** East 4.4 9 5 - & gt; 4.4 9 ** East 4.496 - & gt; 4.50
function ends in myRound (n) {// if .nn5, then round if (/ \. \ D \ d5 $ /. Test ('' + N)) {n = Math.floor (n * 100) / 100; } // apply normal spherical return n.toFixed (2); } Console.log (myRound (4.494)); // 4.49 console.log (myRound (4.495)); // 4.49 console.log (myRound (4.496)); // 4.50
Comments
Post a Comment