1.ceil() 对数进行上舍入
Math.ceil(25.1) =>26
Math.ceil(25.9) =>26
Math.ceil(-25.9) => -25
//因为-25比-25.9的值要大
//负数-号后面的数字越大 值越小
2.floor() 对数进行下舍入
//等于帮你把小数点后面的去掉了
Math.floor(25.9) =>25
Math.floor(25.1) =>25
Math.floor(25.16) =>25
Math.floor(-25.4) => -26
//因为-26比-25.4的值要小
//负数-号后面的数字越大 值越小
3.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
Math.round(-25.5) => -25
★特殊点 满足两个条件 第一个是负数 第二个小数位是5
3.random() 返回0.0~1.0之间的随机数,包括0,但是不包括1
- 公式 Math.floor( Math.random()*(max-min) ) + min
- 公式 Math.floor( Math.random()*(max-min+1) ) + min
<1>题目:使用Math对象随机产生10到100的十个数字,(不包括100)并对这十个随机数排序
let arr=[];//用来存随机数
for(var i=0;i<10;i++){
let num=Math.floor(Mate.randow()*(100-10))+10;
arr.push(num);
}
//排序
arr.sort(function(a,b)){
returu a-b;
})
console.log(arr)
//冒泡排序
for(var a in arr){
for(var b in arr){
if(arr[a]<arr[b]){
var temp=arr[a];
arr[a]=arr[b];
arr[b]=temp;
}
}
}
console.log(arr);
<2>题目:使用Math对象随机产生10到100的十个数字,(包括10,也包括100)并对这十个随机数排重
let arr1=[];//用来存放去重后的数据
for(var j=0;j<10;j++){
if(arr1.indexOf(arr[j])==-1){
//indexOf()返回某个指定的字符串值在字符串中首次出现的位置
arr1.push(arr[j])
//push在末尾添加
}
}
console.log(arr1);
// 包括10 也包括100
arr1.push( Math.floor( Math.random()*(100-10+1) )+10 );
//不包括10 也不包括100 取10-100之间的
arr1.push( Math.floor( Math.random()*(99-11+1) ) + 11 );
//1-10 不包括 1 和10
arr1.push( Math.floor( Math.random()*(9-2+1) ) + 2 );