Math&Date

xiangMath

1、random     返回0~1的随机数,不包含1

//语法:Math.random()
console.log(Math.random);        //得到一个0~1的随机数

2、round    四舍五入

//语法:Math.round(需要计算的数)
console.log(Math.round(1.6));      //2
console.log(Math.round(1.4));      //1

3、abs  取绝对值

//语法:Math.abs(要计算的数)
console.log(Math.abs(-2));      //2
console.log(Math.abs(2));      //2

4、floor  向下取整

//语法:Math.floor(要计算的数)
console.log(Math.floor(1.9));     //1
console.log(Math.floor(1.1));     //1

5、ceil   向上取整

//语法:Math.ceil(要计算的数)
console.log(Math.ceil(1.1));      //2
console.log(Math.ceil(1.9));      //2

6、max   取多个数中的最大值

//语法:Math.max(多个数)
console.log(Math.max(1,2,4,55,66,))    //66  输出多个数中最大的数

7、min  取多个数中最小值

//语法:Math.min(多个数)
console.log(Math.min(1,2,3,40,0));    //0  输出多个数中最小值

8、PI  圆周率

//语法:Math.PI
console.log(Math.PI)    //3.141592653589793

9、pow  返回开次幂的值

//语法:Math.pow(这个数,的多少次方)
console.log(Math.pow(2,3))       //8  输出开次方后的值

10、sqrt  返回开平方后的值

//语法:Math.sqrt(要计算的数)
console.log(Math.sqrt(16));     //4  开平方后的值

       相关例题:

//相关例题:

//0~10的随机整数(不取10)
console.log(Math.floor(Math.random()*10));

//0~10的随机整数(取10)
console.log(Math.floor(Math.random()*11));

//10~20的随机整数(取20)
console.log(Math.floor(Math.random()*11)+10);

//30~50的随机整数
console.log(Math.floor(Math.random()*21)+30);

//n~m的随机整数
function get(n,m){
    return Math.floor(Math.random()*m-n+1)+n
}
console.log(get(n,m))

转换进制

【数字是临时包装数据类型】

1、toString   转换进制

//语法:数字.toString(要转换的进制)   可转换的范围:二进制~三十六进制
var num = 10
console.log(num.toString(2));    //1010
console.log(num.toString(8));    //12
console.log(num.toString(16));   //a

var num1 = 20
console.log(num1.toString(11));   //19
var num2 = 100
console.log(num2.toString(36));   //2s

2、parseint   返回一个十进制的整数

//语法:parseInt(要转换的数字或字符串,把前面这个数看做几进制)
//  若不传第二参数,则把第一个参数当做十进制看待
console.log(parseInt('1a'));    //1
console.log(parseInt('1a',16));   //26

3、toFixed  保留几位小数

//语法:数字.toFixed(保留几位小数)
//返回值为字符串
var num = 12.3335
console.log(num.toFixed(2));    //12.33
console.log(num.toFixed(3));    //12.334    遵循四舍五入

时间对象

【Date是js内置的构造函数,可通过new Date()创建时间对象,若不传参则表示当前时间】

若传递参数:

  1. 第一个参数     YYYY     表示年份;
  2. 第二个参数      MM         表示月份  0~11  0表示1月份  11表示12月份;
  3. 第三个参数      DD          表示日   一个月中的第几天   1~31;
  4. 第四个参数      HH          表示小时   0~23;
  5. 第五个参数      mm          表示分钟   0~59;
  6. 第六个参数      ss          表示小时   0~59;

时间方法

1、getFullYear()    得到时间对象的年份

var time = new.Date()

console.log(time.getFullYear());

2、getMonth()   得到月份  0~11  0表示1月份  11表示12月份

var time = new.Date()

console.log(time.getMonth());

3、getDate()  得到日期

var time = new.Date()

console.log(time.getDate());

4、getHours()   得到小时

var time = new.Date()

console.log(time.getHours());

5、getMinutes()   得到分钟

var time = new.Date()

console.log(time.getMinutes());

6、getSeconds()    得到秒数

var time = new.Date()

console.log(time.getSeconds());

7、getDay()   得到周几  0~6  0表示周日   6表示周六

var time = new.Date()

console.log(time.getDay());

8、getTime()    得到时间戳  得到的是这个时间到格林威治时间的毫秒数

var time = new.Date()

console.log(time.getTime));

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值