JS除法取整

来源:互联网 发布:密码正确无法加入网络 编辑:程序博客网 时间:2024/04/29 22:08

Math.round(x) 四舍五入,如Math.round(0.70),结果为1Math.round(0.39),结果为0;  

Math.floor(x) 向下舍入,如Math.floor(0.70)Math.floor(0.39),结果均为0;  

Math.ceil(x)向上舍入,如Math.ceil(0.70)Math.ceil(0. 39),结果均为1;

parseInt(x)丢弃小数部分,保留整数部分,parseInt(3.14),结果为3。

 

 Math.floor():

_.each(cart_items,function(key){        var promotions_barcode=promotions_items[0].barcodes;        if(promotions_barcode.indexOf(key.barcode)!=-1){            key.promotions_count=Math.floor(key.count/3);//获取3的整数倍,向下取整        }        else{            key.promotions_count=0;        }    });

 parseInt():

 

function getResult(num){return parseInt(num);}

  Math.ceil():

 

function getResult(num,n){return Math.ceil(num);}

 

0 0
原创粉丝点击