JavaScript舍入到Excel等负小数位

时间:2022-02-17 16:36:33

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

我试图在JavaScript中舍入一个数字,以匹配以下Excel样式舍入。

Excel:

=ROUND(123456,-1)

which outputs

123460

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

如何将JavaScript舍入到负小数位?

2 个解决方案

#1


3  

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

#2


0  

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

Formula.js实现Microsoft Excel 2013提供的所有数学函数,包括ROUND。

#1


3  

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

#2


0  

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

Formula.js实现Microsoft Excel 2013提供的所有数学函数,包括ROUND。