Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript rounding to negative decimal places like Excel

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?

like image 367
dibs Avatar asked Dec 05 '25 08:12

dibs


1 Answers

function round(x, places)
{
    var shift = Math.pow(10, places);
    return Math.round(x * shift) / shift;
}
like image 86
Kendall Frey Avatar answered Dec 07 '25 21:12

Kendall Frey



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!