1. Math 對(duì)象
1.1 介紹
Math 對(duì)象,是數(shù)學(xué)對(duì)象,提供對(duì)數(shù)據(jù)的數(shù)學(xué)計(jì)算,如:獲取絕對(duì)值、向上取整等。無(wú)構(gòu)造函數(shù),無(wú)法被初始化,只提供靜態(tài)屬性和方法。
1.2 構(gòu)造函數(shù)
無(wú) :Math 對(duì)象無(wú)構(gòu)造函數(shù),無(wú)法被初始化,只提供靜態(tài)屬性和方法。
1.3 靜態(tài)屬性
1.3.1 Math.E :常量e。返回自然對(duì)數(shù)的底數(shù):2.718281828459045
1.3.2 Math.PI :常量π。返回圓周率的值 :3.141592653589793
1.4 靜態(tài)方法
1.4.1 Math.sin(value) :正弦函數(shù)
1.4.2 Math.cos(value) :余弦函數(shù)
1.4.3 Math.tan(value) :正切函數(shù)
1.4.4 Math.asin(value) :反正弦函數(shù)
1.4.5 Math.acos(value) :反余弦函數(shù)
1.4.6 Math.atan(value) :反正切函數(shù)
1.4.7 Math.abs(value) :返回絕對(duì)值
參數(shù):
①value {Number | NumberStr} :數(shù)字或者純數(shù)字的字符串。
返回值:
{Number} 返回參數(shù)的絕對(duì)值數(shù)字。若參數(shù)不為數(shù)字,返回NaN。
示例:
h.abs('123'); // => 123 :純數(shù)字字符串
Math.abs('-123'); // => 123
Math.abs(123); // => 123
Math.abs(-123); // => 123
Math.abs('123a'); // => NaN :非純數(shù)字字符串
1.4.8 Math.ceil(value) : 對(duì)一個(gè)數(shù)向上取整,并不是四舍五入
參數(shù):
①value {Number | NumberStr} :數(shù)字或者純數(shù)字的字符串。
返回值:
{Number} 返回取整后的值。若參數(shù)不為數(shù)字,返回NaN。
示例:
Math.ceil(2.7); // => 3
Math.ceil(2.3); // => 3 :2.3 向上取整返回 3
Math.ceil(-2.7); // => -2
Math.ceil(-2.3); // => -2
Math.ceil('2.7'); // => 3 :純數(shù)字字符串
Math.ceil('2.7a'); // => NaN :非純數(shù)字字符串
1.4.9 Math.floor(value) :對(duì)一個(gè)數(shù)向下取整,并不是四舍五入
參數(shù):
①value {Number | NumberStr} :數(shù)字或者純數(shù)字的字符串。
返回值:
{Number} 返回取整后的值。若參數(shù)不為數(shù)字,返回NaN。
示例:
Math.floor(2.7); // => 2
Math.floor(2.3); // => 2
Math.floor(-2.7); // => -3 :-2.7 向下取整返回 -3
Math.floor(-2.3); // => -3
Math.floor('2.7'); // => 2 :純數(shù)字字符串
Math.floor('2.7a'); // => NaN :非純數(shù)字字符串
1.4.10 Math.max(value1,value2...valueN) :返回參數(shù)中最大的值
參數(shù):
①value1,value2.....valueN {Number | NumberStr} :數(shù)字或者純數(shù)字的字符串。
返回值:
{Number} 返回最大值。若一個(gè)參數(shù)不為數(shù)字,返回NaN。
示例:
Math.max(1, 2, 3, 4, 5); // => 5
Math.max(1, 2, 3, 4, '5' ); // => 5
Math.max(1, 2, 3, 4, 'a'); // => NaN
1.4.11 Math.min(value1,value2...valueN) :返回參數(shù)中最小的值
參數(shù):
①value1,value2.....valueN {Number | NumberStr} :數(shù)字或者純數(shù)字的字符串。
返回值:
{Number} 返回最大值。若一個(gè)參數(shù)不為數(shù)字,返回NaN。
示例:
Math.min(1, 2, 3, 4, 5); // => 1
Math.min('1', 2, 3, 4, 5); // => 1
Math.min(1, 2, 3, 4, 'a'); // => NaN
1.4.12 Math.pow(x,y) :返回x的y次方
參數(shù):
①x {Number | NumberStr} :數(shù)字或者純數(shù)字的字符串。
②y {Number | NumberStr} :數(shù)字或者純數(shù)字的字符串。
返回值:
{Number} 返回x的y次方。若一個(gè)參數(shù)不為數(shù)字,返回NaN。
示例:
Math.pow(2, 3); // => 8 :2的3次方
Math.pow(3, 2); // => 9 :3的2次方
Math.pow('4', 2); // => 16 :4的2次方
Math.pow('2a', 2); // => NaN
1.4.13 Math.random() :返回一個(gè)偽隨機(jī)數(shù),大于0,小于1.0
參數(shù):無(wú)
返回值:
{Number} 返回一個(gè)偽隨機(jī)數(shù),大于0,小于1.0
示例:
Math.random(); // => 0.8982374747283757
Math.random(); // => 0.39617531932890415
Math.random(); // => 0.35413061641156673
Math.random(); // => 0.054441051790490746
1.4.14 Math.round(value) : 四舍五入后取整
參數(shù):
①value {Number | NumberStr} :數(shù)字或者純數(shù)字的字符串。
返回值:
{Integer} 返回參數(shù)四舍五入后的整數(shù)。若參數(shù)不為數(shù)字,返回NaN。
示例:
Math.round(2.5); // => 3
Math.round(2.4); // => 2
Math.round(-2.6); // => -3
Math.round(-2.5); // => -2 :-2.5四舍五入為 -2
Math.round(-2.4); // => -2
Math.round('2.7'); // => 3 :純數(shù)字字符串
Math.round('2.7a'); // => NaN :非純數(shù)字字符串
1.4.15 Math.sqrt(value) :返回參數(shù)的平方根
參數(shù):
①value {Number | NumberStr} :數(shù)字或者純數(shù)字的字符串
返回值:
{Number} 返回參數(shù)的平方根
示例:
console.log( Math.sqrt(9) ); // => 3
console.log( Math.sqrt(16) ); // => 4
console.log( Math.sqrt('25') ); // => 5
console.log( Math.sqrt('a') ); // => NaN
2. Number 對(duì)象
2.1 介紹
Number 對(duì)象,是數(shù)字對(duì)象,包含js中的整數(shù)、浮點(diǎn)數(shù)等等。
2.2 定義
var a = 1;
var b = 1.1;
2.3 靜態(tài)屬性
2.3.1 Number.MAX_VALUE :表示JS中最大的數(shù)字,約為 1.79e+308
2.3.2 Number.MIN_VALUE :表示JS中最小的數(shù)字,約為 5e-324
2.3.3 Number.NaN :返回NaN,表示非數(shù)字值,與任意其他數(shù)字不等,也包括NaN本身。應(yīng)使用Number.isNaN() 來(lái)進(jìn)行判斷。
2.3.4 Number.NEGATIVE_INFINITY :返回 -Infinity ,表示負(fù)無(wú)窮。
2.3.5 Number.POSITIVE_INFINITY :返回 Infinity ,表示正無(wú)窮。進(jìn)行計(jì)算的值大于Number.MAX_VALUE就返回 Infinity 。
2.4 靜態(tài)方法
2.4.1 Number.isInteger(value) :判斷參數(shù)是否為整數(shù)
參數(shù):
①value {Number} :數(shù)字
返回值:
{Boolean} 返回參數(shù)是否為整數(shù) 。純整數(shù)的字符串也返回false。
示例:
Number.isInteger(1); // => true
Number.isInteger(1.1); // => false
Number.isInteger('1'); // => false :純整數(shù)的字符串也返回false
Number.isInteger('1.1'); // => false
Number.isInteger('a'); // => false :非字符串返回false
2.4.2 Number.isNaN(value) :判斷參數(shù)是否為NaN
參數(shù):
①value {Object} :任意類(lèi)型
返回值:
{Boolean} 返回參數(shù)是否為NaN 。
示例:
Number.isNaN(NaN); // => true
Number.isNaN('NaN'); // => false :'NaN'字符串,并不為NaN
Number.isNaN(1); // => false
Number.isNaN('1'); // => false
2.4.3 Number.parseFloat(value) :把參數(shù)轉(zhuǎn)換為浮點(diǎn)數(shù)
參數(shù):
①value {Number | NumberStr} :數(shù)字或者純數(shù)字的字符串
返回值:
{Integer | Float} 返回整數(shù)或浮點(diǎn)數(shù)數(shù)值
示例:
Number.parseFloat(1); // => 1 :整數(shù)還是返回整數(shù)
Number.parseFloat(1.1); // => 1.1
Number.parseFloat('1aaa'); // => 1 :字符串前面為數(shù)字的,只返回?cái)?shù)字
Number.parseFloat('1.1aaa'); // => 1.1
Number.parseFloat('a1'); // => NaN :非數(shù)字開(kāi)頭,返回NaN
Number.parseFloat('a'); // => NaN
2.4.4 Number.parseInt(value) :把參數(shù)轉(zhuǎn)換為整數(shù)
參數(shù):
①value {Number | NumberStr} :數(shù)字或者純數(shù)字的字符串
返回值:
{Integer} 返回整數(shù)數(shù)值
示例:
Number.parseInt(1); // => 1
Number.parseInt(1.1); // => 1 :浮點(diǎn)數(shù)返回整數(shù)
Number.parseInt('1aaa'); // => 1 :字符串前面為數(shù)字的,只返回?cái)?shù)字
Number.parseInt('1.1aaa'); // => 1
Number.parseInt('a1'); // => NaN :非數(shù)字開(kāi)頭,返回NaN
Number.parseInt('a'); // => NaN
2.5 實(shí)例方法
2.5.1 toExponential(value) :將一個(gè)數(shù)字轉(zhuǎn)為指數(shù)類(lèi)型,參數(shù)表示小數(shù)點(diǎn)后的位數(shù)
參數(shù):
①value {Number} :表示小數(shù)點(diǎn)后的位數(shù)
返回值:
{String} 返回轉(zhuǎn)換后的指數(shù)類(lèi)型字符串
示例:
(123456789).toExponential(2); // => 1.23e+8 :小數(shù)點(diǎn)2位
(123456789).toExponential(5); // => 1.23457e+8 :小數(shù)點(diǎn)5位
(123456789).toExponential(10); // => 1.2345678900e+8 :小數(shù)點(diǎn)10位,不足位數(shù)用0補(bǔ)位
2.5.2 toFixed(value) :將一個(gè)數(shù)字轉(zhuǎn)換為指定小數(shù)位數(shù)的字符串。不傳入?yún)?shù),就是沒(méi)小數(shù)位。返回值為四舍五入
參數(shù):
①value {Number} :表示小數(shù)點(diǎn)后的位數(shù)
返回值:
{String} 返回轉(zhuǎn)換后的字符串;不夠小數(shù)位以0填充;返回值為四舍五入后的值
示例:
console.log((1).toFixed(2)); // => 1.00
console.log((1.2).toFixed(2)); // => 1.20 :不足位數(shù),以0補(bǔ)位
console.log((1.277).toFixed(2)); // => 1.28 :進(jìn)行了四舍五入
2.5.3 toString() :使用指定的進(jìn)制,將一個(gè)數(shù)字轉(zhuǎn)換為字符串。不傳入?yún)?shù),默認(rèn)為十進(jìn)制。
參數(shù):
①value {Number} :表示進(jìn)制數(shù),取值范圍:2到36
返回值:
{String} 轉(zhuǎn)換后進(jìn)制的字符串
示例:
(10).toString(); // => 10 :默認(rèn)為十進(jìn)制
(10).toString(2); // => 1010 :二進(jìn)制
(10).toString(10); // => 10 :十進(jìn)制
(10).toString(16); // => a :十六進(jìn)制
2.6 應(yīng)用場(chǎng)景
2.6.1 浮點(diǎn)數(shù)的加減乘除異常
說(shuō)明:Js中的2個(gè)浮點(diǎn)數(shù)進(jìn)行加減乘除運(yùn)算,會(huì)返回異常的數(shù)值,如:0.2+0.7,返回0.899999999999。可以使用toFixed()方法,指定小數(shù)位。
示例:
console.log(0.2 + 0.7); // => 0.8999999999999999
console.log(0.7 - 0.5); // => 0.19999999999999996
console.log(3.03 * 10); // => 30.299999999999997
// 使用toFixed()方法
console.log( (0.2 + 0.7).toFixed(2) ); // => 0.90
console.log( (0.7 - 0.5).toFixed(2) ); // => 0.20
console.log( (3.03 * 10).toFixed(2) ); // => 30.30
2.6.2 減法運(yùn)算
說(shuō)明:Js中進(jìn)行減法運(yùn)算時(shí),會(huì)先把前后的值轉(zhuǎn)換為數(shù)值再進(jìn)行運(yùn)算。若轉(zhuǎn)換失敗,返回NaN。
示例:
console.log('1' - 0); // => 1 :純數(shù)字字符串減去0,可以快速轉(zhuǎn)換為Nubmer對(duì)象
console.log( ('1' - 0).toFixed(2) ); // => 1.00 :快速轉(zhuǎn)換為Nubmer對(duì)象后調(diào)用實(shí)例方法
console.log('1' - 'a'); // => NaN :一方無(wú)法轉(zhuǎn)換為Nubmer對(duì)象