divide number with decimals javascript

前端 未结 8 2100
误落风尘
误落风尘 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:21

    please check the below code,

    function Dividently(Amount, Quantity) {
            var m = Amount* 100,
                n = m % Quantity,
                v = Math.floor(m / Quantity) / 100,
                w = Math.floor(m / Quantity+ 1) / 100;
    
            for (var i = 0, out = new Array(Quantity); i < Quantity; ++i) {
                out[i] = i < n ? w : v;
            }
            return out;
        }    
    
     var arr = Dividently(3856, 3);    
    

    Math.floor() - Round a number downward to its nearest integer

提交回复
热议问题