Rounding JavaScript to negative decimal places like Excel

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

Excel:

=ROUND(123456,-1)

      

which outputs

123460

      

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

+3


source to share


2 answers


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

      



+3


source


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



0


source







All Articles