欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  web前端

js运算取整 与字符串分割

程序员文章站 2022-03-29 14:10:13
...
【取整】
①parseInt(解析整型,丢弃小数部分,保留整数部分)
语法:var a =parseInt(5/2);
console.log(a) 输出2

②Math.ceil(向上取整,有小数就整数部分加1)
语法:Math.ceil(5/2) 输出3

③Math.round(四舍五入)
语法:Math.round(5/2) 输出3

④Math.floor(向下取整)
Math.floor(5/2) 输出2


【字符串分割】
方法一:分割字符串:string.substring(begin,end) 从begin开始到end,不算begin
实例:
substring=ITEM000003-2;
for(var i=0;i<inputs.length;i++){
if(inputs[i].length==10){
items_1.push(inputs[i]);
}else{
for(var j=0;j<inputs[i].substring(11,12);j++){
items_1.push(inputs[i].substring(0,10));--->从0到10,不算0
}
}
}

方法二:split() 方法将字符串分割为字符串数组,并返回此数组
stringObject.split(separator,limit)
注意:如果把空字符串 ("") 用作 separator,那么 stringObject 中的每个字符之间都会被分割
实例:var ch ='i,like,eat-chicken' ;
console.log(ch.split(','))