Number

一 概念


#### 1、常用数字

```js
整数:10
小数:3.14
科学计数法:1e5 | 1e-5
正负无穷:Infinity | -Infinity
```

#### 2、常用进制

```js
二进制:0b1010
八进制:012
十进制:10
十六进制:0xA
```

#### 3、NaN

```js
非数字类型,通过isNaN()进行判断
```

#### 4、常用常量

```js
最大值:MAX_VALUE(1.7976931348623157e+308)
最小值:MIN_VALUE(5e-324)
正负无穷:NEGATIVE_INFINITY | POSITIVE_INFINITY(Infinity | -Infinity)
```

#### 5、常用实例方法

```js
toExponential(n) => 3.14.toExponential(1) => 3.1e+0 (先科学记数,再确定精度,n为小数精度)
toFixed(n) => 3.14.toFixed(1) => 3.1 (先确定精度,再普通记数,n为小数精度)
toPrecision(n) => 13.14.toPrecision(1|2) => 1e+1|13 (先确定精度,再记数,n为位数精度)
toString(n) => 转换为指定进制的字符串,n代表进制
```

###### v-hint:经典bug数字13.145

二 代码示范

<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<title>Number</title>
</head>
<body>
Number
</body>
<script type="text/javascript">
// 常用数字
console.log(10);
console.log(3.14);
console.log(1e5);
console.log(1e-5);
console.log(Infinity);
console.log(-5/0);

// 进制
var a = 0b1010; // 8421
var b = 012;
var c = 10;
var d = 0xA;
console.log(a, b, c, d);

// 常用常量
console.log(Number.MAX_VALUE);
console.log(Number.MIN_VALUE);
console.log(Number.NEGATIVE_INFINITY, Number.POSITIVE_INFINITY);

console.log(999999999999998);
console.log(9999999999999989); // 9999999999999988
console.log(3.1, 3.2);
console.log(3.1 + 3.2); // 6.300000000000001

// 常用实例方法
// toExponential(n): (先科学记数,再确定精度,n为小数精度)
console.log(13.145.toExponential(3)); // 13.14 => 1.314e+1 => 1.31e+1 (13.15=>1.32e+1)

// toFixed(n): (先确定精度,再普通记数,n为小数精度)
console.log(3.14.toFixed(1)) // 3.14四舍五入保留一位小数

// toPrecision(n): (先确定精度,再记数,n为位数精度)
console.log(13.14.toPrecision(1)) // 1e+1
console.log(13.14.toPrecision(2)) // 13
console.log(13.14.toPrecision(3)) // 13.1
console.log(156.14.toPrecision(1)) // 2e+2

var num = 10;
console.log(num.toString());
console.log(10..toString(8));


</script>
</html>

 

posted @ 2018-10-17 17:42  不沉之月  阅读(207)  评论(0编辑  收藏  举报