javascript记忆

时间:2023-03-10 04:04:30
javascript记忆

Math.round()和ToFixed()

Math.round(1.6)=2

Math.round(-1.4)=-1

var k = 1.74.toFixed(1), m = 1.75.toFixed(1), n = 1.76.toFixed(1),i=-1.49.ToFixed(1);   结果 1.7,1.8,1.8,-1.5

当为负数时round得到的结果可能不是我们想要的所以需要添加绝对值

num Math.floor Math.round Math.ceil
1.4 1 1 2
1.5 1 2 2
1.6 1 2 2
-1.4 -2 -1 -1
-1.5 -2 -1 -1
-1.6 -2 -2 -1

substring(start,end)和substr(start,total)

var s1 = new String("010-9898999");

alert(s1.substring(1,4)); //10-

alert(s1.substr(1,4));//10-9