Home » excel » JavaScript rounding to negative decimal places like Excel

JavaScript rounding to negative decimal places like Excel

Posted by: admin April 23, 2020 Leave a comment

Questions:

I am trying to round a number in JavaScript to match the following Excel style rounding.

Excel:

=ROUND(123456,-1)

which outputs

123460

How can I get JavaScript to round to negative decimal places?

How to&Answers:
function round(x, places)
{
    var shift = Math.pow(10, places);
    return Math.round(x * shift) / shift;
}

Answer:

Formula.js implements all Math functions offered by Microsoft Excel 2013, including ROUND.