Javascript VS PHP rounding

I am having some problems with how round php numbers and javascript are. I am using a PHP round function and this javascript function:

function roundNumber(number, decimals) { var newnumber = new Number(number+'').toFixed(parseInt(decimals)); var value = parseFloat(newnumber); return value; } 

The number I'm trying to round is 43.65 * 2.5 + 40% when this is done using the calculator = 152.775 or when rounding in PHP = 152.78.

In javascript, when I do console.log, the number is 152.774999999998 and when rounding off with the function above gives me 152.77

Any help in spreading this issue is much appreciated

+6
source share
6 answers

Please see How do I deal with precision floating point numbers in JavaScript?

Here is an example

 function roundNumber(number, decimals) { decimals = parseInt(decimals,10); var dec = Math.pow(10,decimals) console.log(dec,parseFloat(number)*dec); number=""+Math.round(parseFloat(number)*dec+.0000000000001); // fixed the .X99999999999 return parseFloat(number.slice(0,-1*decimals) + "." + number.slice(-1*decimals)) } var val = 43.65 * 2.5; val+= val*0.40 console.log(val+' ~= 152.78? --> '+roundNumber(val,2).toFixed(2)); console.log('15.803 ~= 15.80? --> '+roundNumber(15.803,2).toFixed(2)); console.log('15.805 ~= 15.81? --> '+roundNumber(15.805,2).toFixed(2)); console.log('14.803 ~= 14.80? --> '+roundNumber(14.803,2).toFixed(2)); console.log('0.575 ~= 0.58? --> '+roundNumber(0.575,2).toFixed(2)); 
+2
source

This is not due to rounding as such, but due to the way decimal numbers are represented in binary equipment.

Check out the floating point for more information on this, as well as solutions / alternatives.

+5
source

I thought about this a bit and wanted to share my decision, let it not go in vain:

 function roundNumber(number, decimals) { var d = parseInt(decimals,10), dx = Math.pow(10,d), n = parseFloat(number), f = Math.round(Math.round(n * dx * 10) / 10) / dx; return f.toFixed(d); } 

It does not use string functions or any forced rounding up or down.

Test it here: http://jsfiddle.net/inti/hMrsp/4/

Edit: fixed, reduced zeros at the end

+3
source

php rounds to 152.78 because it sees 152.77499, which is 152.775 and at the end 152.178. can't you use rounded value from php?

+1
source

This is due to the fact that the various values ​​used in JS (browser) and PHP, or in fact, how many bits are used to store numbers.

you can make your JS rounding function to round it to the second digit Math.round(floatNumber*100)/100

+1
source

Find below javascript function for number format

 <script type="text/javascript"> function format_number(pnumber,decimals){ if (isNaN(pnumber)) { return 0}; if (pnumber=='') { return 0}; var snum = new String(pnumber); var sec = snum.split('.'); var whole = parseFloat(sec[0]); var result = ''; if(sec.length > 1){ var dec = new String(sec[1]); dec = String(parseFloat(sec[1])/Math.pow(10,(dec.length - decimals))); dec = String(whole + Math.round(parseFloat(dec))/Math.pow(10,decimals)); var dot = dec.indexOf('.'); if(dot == -1){ dec += '.'; dot = dec.indexOf('.'); } while(dec.length <= dot + decimals) { dec += '0'; } result = dec; } else{ var dot; var dec = new String(whole); dec += '.'; dot = dec.indexOf('.'); while(dec.length <= dot + decimals) { dec += '0'; } result = dec; } return result; } var value = format_number(newnumber,2); </script> 
-1
source

Source: https://habr.com/ru/post/891089/


All Articles