Skip to content

Commit

Permalink
11
Browse files Browse the repository at this point in the history
  • Loading branch information
squid-Xu committed Jul 21, 2024
1 parent 058633e commit 789513a
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 21 deletions.
31 changes: 15 additions & 16 deletions demo/1.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,24 @@
</head>
<body>
<script>
var a = 123;
a = String(a);
console.log(typeof a); //string
console.log(a); // "123"
var a = '123.56px';
parseInt(a) //123
parseFloat(a) //123.56

var a = true;
a = String(a);
console.log(typeof a); //string
console.log(a); // "true"
a = 'a123.56px';
parseInt(a) //NaN
parseFloat(a) //NaN

var a = null;
a = String(a);
console.log(typeof a); //string
console.log(a); // "null"
a = true;
parseInt(a) //NaN
parseFloat(a) //NaN

var a = undefined;
a = String(a);
console.log(typeof a); //string
console.log(a); // "undefined"
a = 'true';
parseInt(a) //NaN
parseFloat(a) //NaN

console.log(parseInt(a));
console.log(parseFloat(a));
</script>
</body>
</html>
2 changes: 1 addition & 1 deletion javascript/basics/No4.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
- 命名一个标识符时需要遵守如下规则:

> 1. 标识符中可以含有字母、数字、\_、$
> 1. 标识符中可以含有字母、数字、`_``$`
> 2. 标识符不能以数字开头
Expand Down
6 changes: 3 additions & 3 deletions javascript/basics/No6.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# Number

- 在JS中所有的数值都是Number类型
- 在JS中所有的数值都是 `Number` 类型
- 包括整数和浮点数(小数)

- JS中可以表示的数字最大值 `Number.MAX_VALUE`,(最小值`Number.MIN_VALUE`)
> 如果使用Number表示的数字超过了最大值,则会返回一个`Infinity`,标识正无穷
- JS中可以表示的数字最大值 `Number.MAX_VALUE`,最小值`Number.MIN_VALUE`
> 如果使用 `Number` 表示的数字超过了最大值,则会返回一个`Infinity`,标识正无穷
> `Infinity`是一个字面量
Expand Down
2 changes: 1 addition & 1 deletion javascript/basics/No8.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
- Undefined(未定义)类型的值只有一个 `undefined`

> 当声明一个变量,但是并不给变量赋值时,它的值就是undefined
> 当声明一个变量,但是并不给变量赋值时,它的值就是 `undefined`
> `typeof undefined` 返回 `undefined`
90 changes: 90 additions & 0 deletions javascript/basics/No9.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,94 @@ var a = undefined;
a = String(a);
console.log(typeof a); //string
console.log(a); // "undefined"
```

## 将其他的数据类型转换为Number

### 方式一

- 使用 `Number()` 函数

- 字符串 ---> 数字

> 1、如果是纯数字的字符串,则直接将其转换为数字
> 2、如果字符中有非数字的内容,则转换为 `NaN`
> 3、如果字符串是一个空串,或者是一个全是空格的字符串,则转换为 `0`
- 布尔 ---> 数字

> `true``1`
> `false``0`
- null ---> 数字

> `null``0`
- undefined ---> 数字

> `undefined``NaN`
```js
var a = '123';
Number(a) //123

a = '123px'
Number(a) //NaN

a = ''
Number(a) //0

a = ' '
Number(a) //0

a = true
Number(a) //1

a = false
Number(a) //0

a = null
Number(a) //0

a = undefined
Number(a) //NaN
```


### 方式二

- 这种方式专门用来对付字符串

- `parseInt()` 把一个字符串转换为一个整数

> `parseInt()` 可以将一个字符串中的有效整数内容取出来,然后转换为 `Number`
- `parseFloat()` 把一个字符串转换为一个浮点数

> `parseFloat()` 作用和 `parseInt()` 类似,不同的是它可以获得有效的小数

> [!tip]
> 如果对非 `String` 使用 `parseInt()``parseFloat()`,它会先将其转换为 `String`,然后在操作

```js
var a = '123.56px';
parseInt(a) //123
parseFloat(a) //123.56

a = 'a123.56px';
parseInt(a) //NaN
parseFloat(a) //NaN

a = true;
parseInt(a) //NaN
parseFloat(a) //NaN

a = 'true';
parseInt(a) //NaN
parseFloat(a) //NaN
```

0 comments on commit 789513a

Please sign in to comment.