Skip to content

Round to at most 2 decimal places in JavaScript

toFixed()

var num = "987.654321";
parseFloat(num).toFixed(2);


var numb = 987.654321;
numb = numb.toFixed(2);

Math.round()

function roundToTwo(num) {    
    return +(Math.round(num + "e+2")  + "e-2");
}
See also  Design HashMap - Leetcode Challenge - Python Solution

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.