Math对象

118 阅读1分钟

ceil() 对数进行上舍入 Math.ceil(25.1) =>26 Math.ceil(25.9) =>26 负数-号后面的数字越大 值越小 Math.ceil(-25.9) => -25 因为-25比-25.9的值要大

floor() 对数进行下舍入 等于帮你把小数点后面的去掉了 Math.floor(25.9) =>25 Math.floor(25.1) =>25 Math.floor(25.16) =>25 负数-号后面的数字越大 值越小 Math.floor(-25.4) => -26 因为-26比-25.4的值要小

round() 把数四舍五入为最接近的数 Math.round(25.6) => 26 Math.round(25.4) => 25 Math.round(-25.4) => -25 Math.round(-25.6) => -26 Math.round(25.5) => 26 ★特殊点 满足两个条件 第一个是负数 第二个小数位是5
Math.round(-25.5) => -25 */ document.write( Math.round(-25.5) );

random() 返回0.0~1.0之间的随机数 包括0,但是不包括1 */ document.write( Math.random() );

随机出现 1-10 之间的数 包括1 不包括10 公式 Math.floor( Math.random()(max-min) ) + min document.write( Math.floor(Math.random()(10-1)) + 1 );

随机出现 2-10 之间的数 包括2 也包括10 公式 Math.floor( Math.random()(max-min+1) ) + min document.write( Math.floor(Math.random()(10-2+1)) + 2 );