0
点赞
收藏
分享

微信扫一扫

12-JavaScript的Number对象 DAY10 (04.21)

山竹山竹px 2022-04-21 阅读 69
javascript

1.Number对象的创建

(1)字面量

    var num1=10;

(2)new关键字

    var num2=new Number(10);

2.Number对象的精度

JS只有一种数据类型,包含整数和小数(浮点型)

极大的或者极小的数字可以使用科学计数法表示
 e:10的次方/次幂
精度:(1)整数:(不适用于小数或科学计数法)最多15位
	   (2)小数: 最大17位,运算并不保证100%正确
	var i=1000000000000000;
	var j=100000000000000;
	console.log(i);
        console.log(j);
	var m=0.00000000000000001;
	var n=0.000000000000000001;
	console.log(m);
	console.log(n

);

3.Number对象的进制

   进制(十进制 十六进制 八进制 二进制)
  十六进制  逢16进1 前缀ox
  八进制    逢8进1  前缀o
  进制转换 toString(进制)
var a=10;
console.log(a.toString(16));//a
console.log(a.toString(8));//12
console.log(a.toString(2));//1010

4.Number对象的属性

(1)constructor 返回对创建此函数的Number函数的引用

(2).prototype允许向对象添加属性和方法

(3)MAX_VALUE可以表示最大的数

	console.log(Number.MAX_VALUE);

(4)MIN_VALUE可以表示最小的数

	console.log(Number.MIN_VALUE);

(5)NEGATIVE_INFINITY 负无穷大,溢出时返回该值

	console.log(Number.NEGATIVE_INFINITY);//-Infinity

(6)POSITIVE_INFINITY 正无穷大,溢出时返回该值

	console.log(Number.POSITIVE_INFINITY);//Infinity

(7)NAN非数字值

(8)ES6新增的MAX_SAFE.INTEGER JS中最大的安全整数

(9)ES6新增的MIN_SAFE.INTEGER JS中最小的安全整数

(10)EPSILON 表示1和比最接近1且大于1最小的Number之间的差别

5.Number对象的方法

(1)toExponential() 把对象的值转换为指数(科学)计数法

	      var num=100;
	      console.log(num.toExponential());

(2)toFixed() 把数字转换为字符串,结果得小数点后面有几个数字

	  var num1=1.000000002323;
	  console.log(num1.toFixed(5));

(3)toPrecision() 把数字格式化为指定长度

	  console.log(num1.toPrecision(5));

(4)isFinite(参数) 检测指定参数是否为无穷大,参数为NAN或正负无穷大返回false。其他返回true

	   console.log(isFinite(NaN));

(5)isNaN(参数) 判断参数是否不是一个数字

	   console.log(isNaN('a'));

(6).isInteger(参数) 判断给定参数是否为整数

	  var num2=1.1
	  console.log(isInteger(1.1));

(7)isSafeInteger(参数) 判断给定参数是否为安全整数

6.Boolean对象

           Boolean对象用于将非布尔值转换为布尔值(true或false)
	   var a=new Boolean("c");

(1)属性

	    1`constructor 返回对创建此对象的Boolean函数的引用
	    2`prototype  给对象添加属性和方法

(2)方法

	    1`toString() 把布尔值转换为字符串
	       console.log(a.toString());

	     2`valueof() 返回Boolean 转换为原始值
	       console.log(a.valueOf());

7.Math对象

            Math:用于处理数学任务
        E:返回算数常量e,即自然数,对数的底数(约等于2.718)
	LN2:返回2的自然对数(约等于0.693)
	LN10:10的自然对数(约等于2.302)
	 LOG2E:返回以2为底的e的对数(约等于1.44)
	LOG10E:返回以10为底的e的对数(约等于0.434)
	PI:返回圆周率 (约等于3.14)
	SQRT1_2: 返回2的平方根的倒数(约等于0.707)
	SQRT2:返回以10为底的e的对数(约等于0.434) 

(1)pow(x,y)返回x的y次幂

	  console.log(Math.pow(3,10));

(2)exp(x)返回e的x次方

	  console.log(Math.exp(3));

(3)最大值、最小值

	  1`max(1,2……,n);返回所有数中最大的
	      console.log(Math.max(1,2,5,7,80,99));//99
	   2` min(1,2……,n);返回所有数中最小的
	      console.log(Math.min(1,2,5,7,80,99));//1

(4)取值 绝对值、四舍五入、向上取整、向下取整、随机数

	 1`abs(x) 返回x的绝对值
	    console.log(Math.abs(-100));//100
	 2`ceil(x) 对x进行向上取整
	     console.log(Math.ceil(2.5));//3
	 3`floor(x) 对x进行向下取整
	     console.log(Math.floor(2.9));//2
	 4`round(x) 四舍五入
	    console.log(Math.round(2.6));//3
	 5`random(x) 返回0-1之间的随机数
	     console.log(Math.random());
	     console.log(Math.random()*700+300);//取300-1000里面随机的数

8.Data对象的创建

(1)获取当前日期 时间

var oDate1=new Date();//无参数时  获取当前日期时间
console.log(oDate1);

(2)自定义时间

   new Date(year,month,day,hour,minute,second,millisecond)
   参数为纯数字时 表示为毫秒数  返回距离1970年1月1日0点的毫秒数
    var oDate2=new Date(100000);//
    console.log(oDate2);//Thu Jan 01 1970 08:01:40 GMT+0800 (中国标准时间)  计算机中将1970年1月1日0时0份0秒作为时间纪元。

    将年月日作为参数传进去
    var oDate3=new Date(2022,1,1);
    console.log(oDate3);
   时分秒
    var oDate4=new Date(2022,3,29,18,0,0);
    console.log(oDate4)

  时间格式 字符串作为参数
    var oDate5=new Date("2022-4-29,18:00:00");
    console.log(oDate5);

9.Date对象的属性

(1)constructor 返回对创建此对象的 Date 函数的引用
(2)prototype 向对象中添加属性和方法  原型

10.Date对象的方法

   (1)getFullYear() 从 Date 对象以四位数字返回年份。
console.log(oDate1.getFullYear());//2022
   (2)getMonth() 从 Date 对象返回月份 (0 ~ 11)。1月-12月
console.log(oDate1.getMonth());//3
   (3)getDate() 从 Date 对象返回一个月中的某一天 (1 ~ 31)。
console.log(oDate1.getDate());//21
   (4)getDay()	从 Date 对象返回一周中的某一天 (0 ~ 6)。周日-周六
console.log(oDate1.getDay());//4
   (5) getHours() 返回 Date 对象的小时 (0 ~ 23)。
console.log(oDate1.getHours());//17
   (6)getMinutes()	返回 Date 对象的分钟 (0 ~ 59)。
console.log(oDate1.getMinutes());
   (7) getSeconds()	返回 Date 对象的秒数 (0 ~ 59)。
console.log(oDate1.getSeconds());//43
   (8) getMilliseconds() 返回 Date 对象的毫秒(0 ~ 999)。
console.log(oDate1.getMilliseconds());
  (9)getTime() 返回 1970 年 1 月 1 日至今的毫秒数。
    console.log(oDate1.getTime());
    var oDate2=new Date(100000);
    console.log(oDate2.getTime()); 

11.格林威治时间

   getUTCDate()   	根据世界时从 Date 对象返回月中的一天 (1 ~ 31)。
   getUTCDay()	根据世界时从 Date 对象返回周中的一天 (0 ~ 6)。
   getUTCFullYear()	根据世界时从 Date 对象返回四位数的年份。
   getUTCHours() 	根据世界时返回 Date 对象的小时 (0 ~ 23)。
console.log(oDate1.getUTCHours());//9
   getUTCMilliseconds()	根据世界时返回 Date 对象的毫秒(0 ~ 999)。
   getUTCMinutes()	根据世界时返回 Date 对象的分钟 (0 ~ 59)。
   getUTCMonth()	根据世界时从 Date 对象返回月份 (0 ~ 11)。
   getUTCSeconds()	根据世界时返回 Date 对象的秒钟 (0 ~ 59)。
    getYear()	已废弃。 请使用 getFullYear() 方法代替。

12.设置

   setFullYear()	设置 Date 对象中的年份(四位数字)。
    setMonth()	设置 Date 对象中月份 (0 ~ 11)。    
    setDate()	设置 Date 对象中月的某一天 (1 ~ 31)。
    setHours()	设置 Date 对象中的小时 (0 ~ 23)。
    setMinutes()	设置 Date 对象中的分钟 (0 ~ 59)。
    setSeconds()	设置 Date 对象中的秒钟 (0 ~ 59)。    
   setMilliseconds()	设置 Date 对象中的毫秒 (0 ~ 999)。

13. 设置世界时间 格林威治时间

    setUTCFullYear()	根据世界时设置 Date 对象中的年份(四位数字)。
    setUTCDate()	根据世界时设置 Date 对象中月份的一天 (1 ~ 31)。
    setUTCHours()	根据世界时设置 Date 对象中的小时 (0 ~ 23)。
    setUTCMilliseconds()	根据世界时设置 Date 对象中的毫秒 (0 ~ 999)。
    setUTCMinutes()	根据世界时设置 Date 对象中的分钟 (0 ~ 59)。
    setUTCMonth()	根据世界时设置 Date 对象中的月份 (0 ~ 11)。
    setUTCSeconds()	setUTCSeconds() 方法用于根据世界时 (UTC) 
举报

相关推荐

Day10

day10

(Day10)String方法

Java Web day10

day10 - 函数基础

自学Java day10

Java学习Day10

算法练习-day10

0 条评论