js保留位和取整

 1 //hold是保留位,例,元,角,分
 2 //integerType是在保留位的基础上,如果后面有值,向上向下取整
 3 calAmount:function(hold,integerType,amount){
 4         if(hold=='YUAN'){
 5           if(integerType=='ROUND'){
 6             //四舍五入
 7             return amount.toFixed(0);
 8           }else if(integerType=='LASTINTERCEPT'){
 9             //舍0(去掉保留位后面的数字)
10             return parseInt(amount);
11           }else if(integerType=='CARRYOVER'){
12             //进位
13             return Math.ceil(amount);
14           }
16         }else if(hold=='JIAO'){
17           if(integerType=='ROUND'){
18             //四舍五入
19             return amount.toFixed(1);
20           }else if(integerType=='LASTINTERCEPT'){
21             //舍0
22             var am = amount.toString();
23             var nu = am.indexOf('.');
24             if(am.length-nu>2){
25               var am1 = amount.toFixed(2).toString();
26               return am1.substring(0,am1.length-1);
27             }
28             return amount;
29           }else if(integerType=='CARRYOVER'){
30             //进位
31             var am = amount.toString();
32             var nu = am.indexOf('.');
33             if(am.length-nu>2){
34               var am1 = (amount+0.1).toFixed(2).toString();
35               return am1.substring(0,am1.length-1);
36             }
37             return amount;
38           }
40         }else if(hold=='FEN'){
41           if(integerType=='ROUND'){
42             //四舍五入
43             return amount.toFixed(2);
44           }else if(integerType=='LASTINTERCEPT'){
45             //舍0
46             var am = amount.toString();
47             var nu = am.indexOf('.');
48             if(am.length-nu>3){
49               var am1 = amount.toFixed(3).toString();
50               return am1.substring(0,am1.length-1);
51             }
52             return amount;
53           }else if(integerType=='CARRYOVER'){
54             //进位
55             var am = amount.toString();
56             var nu = am.indexOf('.');
57             if(am.length-nu>3){
58               var am1 = (amount+0.01).toFixed(3).toString();
59               return am1.substring(0,am1.length-1);
60             }
61             return amount;
62           }
64         }
65       } 

 

posted @ 2016-10-29 14:48  suruozhong  阅读(910)  评论(0编辑  收藏  举报