Skip to content

数字

JavaScript 对大数字的支持有限。

123456789_123456789
123456789123456780

查看类型。

typeof 123

typeof 1.5

typeof 123n
'number'

'number'

'bigint'

加法。

1 + 2

0.1 + 0.2
3

0.30000000000000004

减法。

7 - 2

0.7 - 0.2
5

0.49999999999999994

乘法。

3 * 4

0.3 * 0.4
12

0.12

除法。

8 / 2

7 / 2

0.8 / 0.2
4

3.5

4

取整。

Number.parseInt(8 / 2, 10)

Number.parseInt(7 / 2, 10)

parseInt(8 / 2, 10)

parseInt(7 / 2, 10)
4

3

4

3

向下取整。

Math.floor(8 / 2)

Math.floor(7 / 2)
4

3

向上取整。

Math.ceil(8 / 2)

Math.ceil(7 / 2)
4

4

舍入。

Math.round(2.5)
3

Number.parseInt() 等效于全局的 parseInt() ,以便模块化。 Number.parseFloat() 等效于全局的 parseFloat() 。

取余。

8 % 2

7 % 2
0

1

幂次。

2 ** 3
8

联系 math@baima.site