Skip to content

数字

一句话解释

数字 是 numeric 值 — JavaScript 有 one 数字 类型 for 两者都 integers and floating-点 值.

用大白话说

JavaScript has a single number type that represents both integers (42) and floating-point values (3.14). Numbers are stored as IEEE 754 double-precision (64-bit) floating point.

这个means integers up to 2^53 are exact, but very large integers and most decimal fractions cannot be represented precisely. For example, 0.1 + 0.2 equals 0.30000000000000004, not 0.3. For large integers, use BigInt.

Useful number methods include toFixed (format decimals), toString (convert to string), parseInt, and parseFloat. The Math object provides Math.floor, Math.round, Math.最大值, Math.random, and more.

示例

javascript
const price = 19.99;
const qty = 3;
console.log(price * qty);          // 59.97
console.log((0.1 + 0.2).toFixed(1)); // "0.3"
console.log(Math.floor(3.7));        // 3

数字 support arithmetic, 格式化, and Math 方法.

常见混淆

  • 容易混淆的对象: BigInt

    关键区别: 数字 是 64-位 float and loses precision above 2^53; BigInt 能 represent arbitrarily 大 integers 精确地 但 cannot 是 used 使用 Math 方法.

相关术语

相关资源

相关课程

解读错误信息

用大白话解释常见错误——什么意思、为什么发生、如何修复。

查看错误

← 返回术语表