3

根据总数值和要分配的个数平均分配,分配不均的分到最后一个值上

 2 years ago
source link: https://segmentfault.com/a/1190000040834911
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

根据总数值和要分配的个数平均分配,分配不均的分到最后一个值上

发布于 今天 10:06

console.log(average(10,3))
image.png

        // value 数值
        // amount 分配数量
        // point 小数点位数
        function average(value,amount,point){
            if(!point){
                point = 2
            }
            let list  = []
            let first = (Math.floor((value/amount)*Math.pow(10,point))/Math.pow(10,point)).toFixed(point)
            let last  = (value - first*(amount - 1)).toFixed(point)
            for(let i = 0;i<amount-1;i++){
                list.push(first)
            }
            list.push(last)
            return list
        }

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK