divide number with decimals javascript

前端 未结 8 2055
误落风尘
误落风尘 2021-01-03 12:54

how can I divide number (money) to x number equally the number could be with one or two decimal or without it

such as 1000 or 100.2 or

8条回答
  •  被撕碎了的回忆
    2021-01-03 13:22

    function divide(numerator, divisor) {
        var results = [];
        var dividend = (Math.floor(numerator/divisor*100)/100).toFixed(2); // dividend with 2 decimal places
        for (var i = 0; i < divisor-1; i++) { 
            results.push(dividend); // Put n-1 copies of dividend in results
        }
        results.push(numerator - (divisor-1)*dividend); // Add remainder to results
        return results;
    }
    

提交回复
热议问题