Javascript VS PHP rounding

后端 未结 6 722
轮回少年
轮回少年 2020-12-18 05:11

I am having some problems with the way PHP and javascript round numbers. I am using PHP\'s round function and this javascript function:



        
相关标签:
6条回答
  • 2020-12-18 05:18

    I was thinking a bit on this, and wanted to share my solution, let it not go to waste:

    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);
    }
    

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

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

    Edit: corrected, was cutting down zeros at the end

    0 讨论(0)
  • 2020-12-18 05:18

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

    0 讨论(0)
  • 2020-12-18 05:27

    Please have a look at How to deal with floating point number precision 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));

    0 讨论(0)
  • 2020-12-18 05:34

    This isn't anything to do with rounding per se, but is to do with how decimal numbers are represented in binary-based hardware.

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

    0 讨论(0)
  • 2020-12-18 05:34

    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>
    
    0 讨论(0)
  • 2020-12-18 05:40

    It is because the different precisions used in JS (by the browser) and PHP or actually how many bits are used to store the numbers.

    you can make your JS rounding function do this to round to the 2nd digit Math.round(floatNumber*100)/100

    0 讨论(0)
提交回复
热议问题