How to round in javascript like PHP do












5















I'm trying to round a float number in Javascript in the same way that I do it in PHP; but I can not make both languages ​​round in the same way the following number:6.404999999999999



When I use PHP round I get: 6.41, but when I trying to round with Javascript I always get 6.40



My Javascript attempts:



Attempt #1:



module.exports = function round (value, precision, mode) {
var m, f, isHalf, sgn // helper variables
// making sure precision is integer
precision |= 0
m = Math.pow(10, precision)
value *= m
// sign of the number
sgn = (value > 0) | -(value < 0)
isHalf = value % 1 === 0.5 * sgn
f = Math.floor(value)

if (isHalf) {
switch (mode) {
case 'PHP_ROUND_HALF_DOWN':
// rounds .5 toward zero
value = f + (sgn < 0)
break
case 'PHP_ROUND_HALF_EVEN':
// rouds .5 towards the next even integer
value = f + (f % 2 * sgn)
break
case 'PHP_ROUND_HALF_ODD':
// rounds .5 towards the next odd integer
value = f + !(f % 2)
break
default:
// rounds .5 away from zero
value = f + (sgn > 0)
}
}

return (isHalf ? value : Math.round(value)) / m
}


Extracted from: http://locutus.io/php/math/round/



Attempt #2:



round(decimal: number, decimalPoints: number): number{
let roundedValue = Math.round(decimal * Math.pow(10, decimalPoints)) / Math.pow(10, decimalPoints);

console.log(`Rounded ${decimal} to ${roundedValue}`);
return roundedValue;
}


Extracted from: https://stackoverflow.com/a/50918962/4359029





I tried with other solutions... but without success.



Could someone tell me how to get the rounding of Javascript to act like PHP's?



Could you tell me why they work in different ways in the case I explain?










share|improve this question




















  • 1





    This might be because PHP does have an accurate binary representation of the number and knows that the number is actually 6.405 but is unable to display it accurately in decimal. this is a good read about this the problem is that in all likelyhood when you send the decimal representation of that number to JavaScript the accurate binary representation is lost and JavaScript has to work with noisy data. I doubt there is anything to do other than send the ops to JavaScript and have it do the calculation from scratch

    – apokryfos
    Nov 23 '18 at 16:59


















5















I'm trying to round a float number in Javascript in the same way that I do it in PHP; but I can not make both languages ​​round in the same way the following number:6.404999999999999



When I use PHP round I get: 6.41, but when I trying to round with Javascript I always get 6.40



My Javascript attempts:



Attempt #1:



module.exports = function round (value, precision, mode) {
var m, f, isHalf, sgn // helper variables
// making sure precision is integer
precision |= 0
m = Math.pow(10, precision)
value *= m
// sign of the number
sgn = (value > 0) | -(value < 0)
isHalf = value % 1 === 0.5 * sgn
f = Math.floor(value)

if (isHalf) {
switch (mode) {
case 'PHP_ROUND_HALF_DOWN':
// rounds .5 toward zero
value = f + (sgn < 0)
break
case 'PHP_ROUND_HALF_EVEN':
// rouds .5 towards the next even integer
value = f + (f % 2 * sgn)
break
case 'PHP_ROUND_HALF_ODD':
// rounds .5 towards the next odd integer
value = f + !(f % 2)
break
default:
// rounds .5 away from zero
value = f + (sgn > 0)
}
}

return (isHalf ? value : Math.round(value)) / m
}


Extracted from: http://locutus.io/php/math/round/



Attempt #2:



round(decimal: number, decimalPoints: number): number{
let roundedValue = Math.round(decimal * Math.pow(10, decimalPoints)) / Math.pow(10, decimalPoints);

console.log(`Rounded ${decimal} to ${roundedValue}`);
return roundedValue;
}


Extracted from: https://stackoverflow.com/a/50918962/4359029





I tried with other solutions... but without success.



Could someone tell me how to get the rounding of Javascript to act like PHP's?



Could you tell me why they work in different ways in the case I explain?










share|improve this question




















  • 1





    This might be because PHP does have an accurate binary representation of the number and knows that the number is actually 6.405 but is unable to display it accurately in decimal. this is a good read about this the problem is that in all likelyhood when you send the decimal representation of that number to JavaScript the accurate binary representation is lost and JavaScript has to work with noisy data. I doubt there is anything to do other than send the ops to JavaScript and have it do the calculation from scratch

    – apokryfos
    Nov 23 '18 at 16:59
















5












5








5


2






I'm trying to round a float number in Javascript in the same way that I do it in PHP; but I can not make both languages ​​round in the same way the following number:6.404999999999999



When I use PHP round I get: 6.41, but when I trying to round with Javascript I always get 6.40



My Javascript attempts:



Attempt #1:



module.exports = function round (value, precision, mode) {
var m, f, isHalf, sgn // helper variables
// making sure precision is integer
precision |= 0
m = Math.pow(10, precision)
value *= m
// sign of the number
sgn = (value > 0) | -(value < 0)
isHalf = value % 1 === 0.5 * sgn
f = Math.floor(value)

if (isHalf) {
switch (mode) {
case 'PHP_ROUND_HALF_DOWN':
// rounds .5 toward zero
value = f + (sgn < 0)
break
case 'PHP_ROUND_HALF_EVEN':
// rouds .5 towards the next even integer
value = f + (f % 2 * sgn)
break
case 'PHP_ROUND_HALF_ODD':
// rounds .5 towards the next odd integer
value = f + !(f % 2)
break
default:
// rounds .5 away from zero
value = f + (sgn > 0)
}
}

return (isHalf ? value : Math.round(value)) / m
}


Extracted from: http://locutus.io/php/math/round/



Attempt #2:



round(decimal: number, decimalPoints: number): number{
let roundedValue = Math.round(decimal * Math.pow(10, decimalPoints)) / Math.pow(10, decimalPoints);

console.log(`Rounded ${decimal} to ${roundedValue}`);
return roundedValue;
}


Extracted from: https://stackoverflow.com/a/50918962/4359029





I tried with other solutions... but without success.



Could someone tell me how to get the rounding of Javascript to act like PHP's?



Could you tell me why they work in different ways in the case I explain?










share|improve this question
















I'm trying to round a float number in Javascript in the same way that I do it in PHP; but I can not make both languages ​​round in the same way the following number:6.404999999999999



When I use PHP round I get: 6.41, but when I trying to round with Javascript I always get 6.40



My Javascript attempts:



Attempt #1:



module.exports = function round (value, precision, mode) {
var m, f, isHalf, sgn // helper variables
// making sure precision is integer
precision |= 0
m = Math.pow(10, precision)
value *= m
// sign of the number
sgn = (value > 0) | -(value < 0)
isHalf = value % 1 === 0.5 * sgn
f = Math.floor(value)

if (isHalf) {
switch (mode) {
case 'PHP_ROUND_HALF_DOWN':
// rounds .5 toward zero
value = f + (sgn < 0)
break
case 'PHP_ROUND_HALF_EVEN':
// rouds .5 towards the next even integer
value = f + (f % 2 * sgn)
break
case 'PHP_ROUND_HALF_ODD':
// rounds .5 towards the next odd integer
value = f + !(f % 2)
break
default:
// rounds .5 away from zero
value = f + (sgn > 0)
}
}

return (isHalf ? value : Math.round(value)) / m
}


Extracted from: http://locutus.io/php/math/round/



Attempt #2:



round(decimal: number, decimalPoints: number): number{
let roundedValue = Math.round(decimal * Math.pow(10, decimalPoints)) / Math.pow(10, decimalPoints);

console.log(`Rounded ${decimal} to ${roundedValue}`);
return roundedValue;
}


Extracted from: https://stackoverflow.com/a/50918962/4359029





I tried with other solutions... but without success.



Could someone tell me how to get the rounding of Javascript to act like PHP's?



Could you tell me why they work in different ways in the case I explain?







javascript php math rounding






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 24 '18 at 9:01







tomloprod

















asked Nov 23 '18 at 16:36









tomloprodtomloprod

4,22443148




4,22443148








  • 1





    This might be because PHP does have an accurate binary representation of the number and knows that the number is actually 6.405 but is unable to display it accurately in decimal. this is a good read about this the problem is that in all likelyhood when you send the decimal representation of that number to JavaScript the accurate binary representation is lost and JavaScript has to work with noisy data. I doubt there is anything to do other than send the ops to JavaScript and have it do the calculation from scratch

    – apokryfos
    Nov 23 '18 at 16:59
















  • 1





    This might be because PHP does have an accurate binary representation of the number and knows that the number is actually 6.405 but is unable to display it accurately in decimal. this is a good read about this the problem is that in all likelyhood when you send the decimal representation of that number to JavaScript the accurate binary representation is lost and JavaScript has to work with noisy data. I doubt there is anything to do other than send the ops to JavaScript and have it do the calculation from scratch

    – apokryfos
    Nov 23 '18 at 16:59










1




1





This might be because PHP does have an accurate binary representation of the number and knows that the number is actually 6.405 but is unable to display it accurately in decimal. this is a good read about this the problem is that in all likelyhood when you send the decimal representation of that number to JavaScript the accurate binary representation is lost and JavaScript has to work with noisy data. I doubt there is anything to do other than send the ops to JavaScript and have it do the calculation from scratch

– apokryfos
Nov 23 '18 at 16:59







This might be because PHP does have an accurate binary representation of the number and knows that the number is actually 6.405 but is unable to display it accurately in decimal. this is a good read about this the problem is that in all likelyhood when you send the decimal representation of that number to JavaScript the accurate binary representation is lost and JavaScript has to work with noisy data. I doubt there is anything to do other than send the ops to JavaScript and have it do the calculation from scratch

– apokryfos
Nov 23 '18 at 16:59














3 Answers
3






active

oldest

votes


















5














You can use toPrecision Function



It will rounded by the precision provided. Here I rounded by 4 to get 6.405 and then rounded by 3 to get 6.41



parseFloat((6.404999999999999).toPrecision(4)).toPrecision(3);





console.log(parseFloat((6.404999999999999).toPrecision(4)).toPrecision(3));








share|improve this answer

































    1





    +50









    You could do it like this:



    function roundToXDigits(value, digits)
    {
    if (!digits) {
    digits = 2;
    }

    value = value * Math.pow(10, digits);
    value = Math.round(value);
    value = value / Math.pow(10, digits);

    return value
    }

    var num = roundToXDigits(6.404999999999999, 3); //creates 6.405

    console.log(roundToXDigits(num, 2)); //creates 6.41


    The problem is, JavaScript technically isn't wrong. 6.4049 -> becomes 6.405 when you round (which, to 2dp is 6.40) which is why it's not working as expected. You have to run the function twice to round 405 -> 41.



    Source: JavaScript math, round to two decimal places



    ^^ extension usage of Bryce's answer.






    share|improve this answer


























    • @tomloprod no prob, hope it helps :)

      – treyBake
      Nov 23 '18 at 16:54











    • treyBake Works well! I think it's similar to @C2486 answer. Guys, what solution do you think is the best?

      – tomloprod
      Nov 23 '18 at 17:08











    • @tomloprod : Its always good to explore any method deeply, but the best practice is to use inbuilt method provided by any language or framework. Its good to see detail code of toPrecision as roundToXDigits.this roundToXDigits method would be great if the inbuilt method is not supported by any version of browser.

      – C2486
      Nov 23 '18 at 17:13





















    0














    You might try



    console.log(Number(n.toFixed(3)).toFixed(2));


    eg.






    var n = 6.404999999999999;
    console.log(n.toFixed(2)); //=> 6.40
    console.log(n.toFixed(3)); //=> 6.405








    share|improve this answer


























    • Have you read the question?

      – Roko C. Buljan
      Nov 23 '18 at 16:58











    • @RokoC.Buljan, There is a fundamental difference between the round method in PHP and javascript. But according the basic definition of the round functionality, the values are equal to results of JavaScript.

      – Chameera
      Nov 23 '18 at 17:14











    • How about this, console.log(Number(n.toFixed(3)).toFixed(2));

      – Chameera
      Nov 23 '18 at 17:19













    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53450248%2fhow-to-round-in-javascript-like-php-do%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    5














    You can use toPrecision Function



    It will rounded by the precision provided. Here I rounded by 4 to get 6.405 and then rounded by 3 to get 6.41



    parseFloat((6.404999999999999).toPrecision(4)).toPrecision(3);





    console.log(parseFloat((6.404999999999999).toPrecision(4)).toPrecision(3));








    share|improve this answer






























      5














      You can use toPrecision Function



      It will rounded by the precision provided. Here I rounded by 4 to get 6.405 and then rounded by 3 to get 6.41



      parseFloat((6.404999999999999).toPrecision(4)).toPrecision(3);





      console.log(parseFloat((6.404999999999999).toPrecision(4)).toPrecision(3));








      share|improve this answer




























        5












        5








        5







        You can use toPrecision Function



        It will rounded by the precision provided. Here I rounded by 4 to get 6.405 and then rounded by 3 to get 6.41



        parseFloat((6.404999999999999).toPrecision(4)).toPrecision(3);





        console.log(parseFloat((6.404999999999999).toPrecision(4)).toPrecision(3));








        share|improve this answer















        You can use toPrecision Function



        It will rounded by the precision provided. Here I rounded by 4 to get 6.405 and then rounded by 3 to get 6.41



        parseFloat((6.404999999999999).toPrecision(4)).toPrecision(3);





        console.log(parseFloat((6.404999999999999).toPrecision(4)).toPrecision(3));








        console.log(parseFloat((6.404999999999999).toPrecision(4)).toPrecision(3));





        console.log(parseFloat((6.404999999999999).toPrecision(4)).toPrecision(3));






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 23 '18 at 17:08

























        answered Nov 23 '18 at 17:01









        C2486C2486

        19.1k32767




        19.1k32767

























            1





            +50









            You could do it like this:



            function roundToXDigits(value, digits)
            {
            if (!digits) {
            digits = 2;
            }

            value = value * Math.pow(10, digits);
            value = Math.round(value);
            value = value / Math.pow(10, digits);

            return value
            }

            var num = roundToXDigits(6.404999999999999, 3); //creates 6.405

            console.log(roundToXDigits(num, 2)); //creates 6.41


            The problem is, JavaScript technically isn't wrong. 6.4049 -> becomes 6.405 when you round (which, to 2dp is 6.40) which is why it's not working as expected. You have to run the function twice to round 405 -> 41.



            Source: JavaScript math, round to two decimal places



            ^^ extension usage of Bryce's answer.






            share|improve this answer


























            • @tomloprod no prob, hope it helps :)

              – treyBake
              Nov 23 '18 at 16:54











            • treyBake Works well! I think it's similar to @C2486 answer. Guys, what solution do you think is the best?

              – tomloprod
              Nov 23 '18 at 17:08











            • @tomloprod : Its always good to explore any method deeply, but the best practice is to use inbuilt method provided by any language or framework. Its good to see detail code of toPrecision as roundToXDigits.this roundToXDigits method would be great if the inbuilt method is not supported by any version of browser.

              – C2486
              Nov 23 '18 at 17:13


















            1





            +50









            You could do it like this:



            function roundToXDigits(value, digits)
            {
            if (!digits) {
            digits = 2;
            }

            value = value * Math.pow(10, digits);
            value = Math.round(value);
            value = value / Math.pow(10, digits);

            return value
            }

            var num = roundToXDigits(6.404999999999999, 3); //creates 6.405

            console.log(roundToXDigits(num, 2)); //creates 6.41


            The problem is, JavaScript technically isn't wrong. 6.4049 -> becomes 6.405 when you round (which, to 2dp is 6.40) which is why it's not working as expected. You have to run the function twice to round 405 -> 41.



            Source: JavaScript math, round to two decimal places



            ^^ extension usage of Bryce's answer.






            share|improve this answer


























            • @tomloprod no prob, hope it helps :)

              – treyBake
              Nov 23 '18 at 16:54











            • treyBake Works well! I think it's similar to @C2486 answer. Guys, what solution do you think is the best?

              – tomloprod
              Nov 23 '18 at 17:08











            • @tomloprod : Its always good to explore any method deeply, but the best practice is to use inbuilt method provided by any language or framework. Its good to see detail code of toPrecision as roundToXDigits.this roundToXDigits method would be great if the inbuilt method is not supported by any version of browser.

              – C2486
              Nov 23 '18 at 17:13
















            1





            +50







            1





            +50



            1




            +50





            You could do it like this:



            function roundToXDigits(value, digits)
            {
            if (!digits) {
            digits = 2;
            }

            value = value * Math.pow(10, digits);
            value = Math.round(value);
            value = value / Math.pow(10, digits);

            return value
            }

            var num = roundToXDigits(6.404999999999999, 3); //creates 6.405

            console.log(roundToXDigits(num, 2)); //creates 6.41


            The problem is, JavaScript technically isn't wrong. 6.4049 -> becomes 6.405 when you round (which, to 2dp is 6.40) which is why it's not working as expected. You have to run the function twice to round 405 -> 41.



            Source: JavaScript math, round to two decimal places



            ^^ extension usage of Bryce's answer.






            share|improve this answer















            You could do it like this:



            function roundToXDigits(value, digits)
            {
            if (!digits) {
            digits = 2;
            }

            value = value * Math.pow(10, digits);
            value = Math.round(value);
            value = value / Math.pow(10, digits);

            return value
            }

            var num = roundToXDigits(6.404999999999999, 3); //creates 6.405

            console.log(roundToXDigits(num, 2)); //creates 6.41


            The problem is, JavaScript technically isn't wrong. 6.4049 -> becomes 6.405 when you round (which, to 2dp is 6.40) which is why it's not working as expected. You have to run the function twice to round 405 -> 41.



            Source: JavaScript math, round to two decimal places



            ^^ extension usage of Bryce's answer.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 23 '18 at 16:54

























            answered Nov 23 '18 at 16:52









            treyBaketreyBake

            3,24531035




            3,24531035













            • @tomloprod no prob, hope it helps :)

              – treyBake
              Nov 23 '18 at 16:54











            • treyBake Works well! I think it's similar to @C2486 answer. Guys, what solution do you think is the best?

              – tomloprod
              Nov 23 '18 at 17:08











            • @tomloprod : Its always good to explore any method deeply, but the best practice is to use inbuilt method provided by any language or framework. Its good to see detail code of toPrecision as roundToXDigits.this roundToXDigits method would be great if the inbuilt method is not supported by any version of browser.

              – C2486
              Nov 23 '18 at 17:13





















            • @tomloprod no prob, hope it helps :)

              – treyBake
              Nov 23 '18 at 16:54











            • treyBake Works well! I think it's similar to @C2486 answer. Guys, what solution do you think is the best?

              – tomloprod
              Nov 23 '18 at 17:08











            • @tomloprod : Its always good to explore any method deeply, but the best practice is to use inbuilt method provided by any language or framework. Its good to see detail code of toPrecision as roundToXDigits.this roundToXDigits method would be great if the inbuilt method is not supported by any version of browser.

              – C2486
              Nov 23 '18 at 17:13



















            @tomloprod no prob, hope it helps :)

            – treyBake
            Nov 23 '18 at 16:54





            @tomloprod no prob, hope it helps :)

            – treyBake
            Nov 23 '18 at 16:54













            treyBake Works well! I think it's similar to @C2486 answer. Guys, what solution do you think is the best?

            – tomloprod
            Nov 23 '18 at 17:08





            treyBake Works well! I think it's similar to @C2486 answer. Guys, what solution do you think is the best?

            – tomloprod
            Nov 23 '18 at 17:08













            @tomloprod : Its always good to explore any method deeply, but the best practice is to use inbuilt method provided by any language or framework. Its good to see detail code of toPrecision as roundToXDigits.this roundToXDigits method would be great if the inbuilt method is not supported by any version of browser.

            – C2486
            Nov 23 '18 at 17:13







            @tomloprod : Its always good to explore any method deeply, but the best practice is to use inbuilt method provided by any language or framework. Its good to see detail code of toPrecision as roundToXDigits.this roundToXDigits method would be great if the inbuilt method is not supported by any version of browser.

            – C2486
            Nov 23 '18 at 17:13













            0














            You might try



            console.log(Number(n.toFixed(3)).toFixed(2));


            eg.






            var n = 6.404999999999999;
            console.log(n.toFixed(2)); //=> 6.40
            console.log(n.toFixed(3)); //=> 6.405








            share|improve this answer


























            • Have you read the question?

              – Roko C. Buljan
              Nov 23 '18 at 16:58











            • @RokoC.Buljan, There is a fundamental difference between the round method in PHP and javascript. But according the basic definition of the round functionality, the values are equal to results of JavaScript.

              – Chameera
              Nov 23 '18 at 17:14











            • How about this, console.log(Number(n.toFixed(3)).toFixed(2));

              – Chameera
              Nov 23 '18 at 17:19


















            0














            You might try



            console.log(Number(n.toFixed(3)).toFixed(2));


            eg.






            var n = 6.404999999999999;
            console.log(n.toFixed(2)); //=> 6.40
            console.log(n.toFixed(3)); //=> 6.405








            share|improve this answer


























            • Have you read the question?

              – Roko C. Buljan
              Nov 23 '18 at 16:58











            • @RokoC.Buljan, There is a fundamental difference between the round method in PHP and javascript. But according the basic definition of the round functionality, the values are equal to results of JavaScript.

              – Chameera
              Nov 23 '18 at 17:14











            • How about this, console.log(Number(n.toFixed(3)).toFixed(2));

              – Chameera
              Nov 23 '18 at 17:19
















            0












            0








            0







            You might try



            console.log(Number(n.toFixed(3)).toFixed(2));


            eg.






            var n = 6.404999999999999;
            console.log(n.toFixed(2)); //=> 6.40
            console.log(n.toFixed(3)); //=> 6.405








            share|improve this answer















            You might try



            console.log(Number(n.toFixed(3)).toFixed(2));


            eg.






            var n = 6.404999999999999;
            console.log(n.toFixed(2)); //=> 6.40
            console.log(n.toFixed(3)); //=> 6.405








            var n = 6.404999999999999;
            console.log(n.toFixed(2)); //=> 6.40
            console.log(n.toFixed(3)); //=> 6.405





            var n = 6.404999999999999;
            console.log(n.toFixed(2)); //=> 6.40
            console.log(n.toFixed(3)); //=> 6.405






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 25 '18 at 7:37

























            answered Nov 23 '18 at 16:51









            ChameeraChameera

            628




            628













            • Have you read the question?

              – Roko C. Buljan
              Nov 23 '18 at 16:58











            • @RokoC.Buljan, There is a fundamental difference between the round method in PHP and javascript. But according the basic definition of the round functionality, the values are equal to results of JavaScript.

              – Chameera
              Nov 23 '18 at 17:14











            • How about this, console.log(Number(n.toFixed(3)).toFixed(2));

              – Chameera
              Nov 23 '18 at 17:19





















            • Have you read the question?

              – Roko C. Buljan
              Nov 23 '18 at 16:58











            • @RokoC.Buljan, There is a fundamental difference between the round method in PHP and javascript. But according the basic definition of the round functionality, the values are equal to results of JavaScript.

              – Chameera
              Nov 23 '18 at 17:14











            • How about this, console.log(Number(n.toFixed(3)).toFixed(2));

              – Chameera
              Nov 23 '18 at 17:19



















            Have you read the question?

            – Roko C. Buljan
            Nov 23 '18 at 16:58





            Have you read the question?

            – Roko C. Buljan
            Nov 23 '18 at 16:58













            @RokoC.Buljan, There is a fundamental difference between the round method in PHP and javascript. But according the basic definition of the round functionality, the values are equal to results of JavaScript.

            – Chameera
            Nov 23 '18 at 17:14





            @RokoC.Buljan, There is a fundamental difference between the round method in PHP and javascript. But according the basic definition of the round functionality, the values are equal to results of JavaScript.

            – Chameera
            Nov 23 '18 at 17:14













            How about this, console.log(Number(n.toFixed(3)).toFixed(2));

            – Chameera
            Nov 23 '18 at 17:19







            How about this, console.log(Number(n.toFixed(3)).toFixed(2));

            – Chameera
            Nov 23 '18 at 17:19




















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53450248%2fhow-to-round-in-javascript-like-php-do%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            404 Error Contact Form 7 ajax form submitting

            How to know if a Active Directory user can login interactively

            Refactoring coordinates for Minecraft Pi buildings written in Python