数学
Math 对象属性
let e = Math.E;
let ln2 = Math.LN2;
let l10 = Math.LN10;
let l2e = Math.LOG2E;
let l1e = Math.LOG10E;
let pi = Math.PI;
let sq1 = Math.SQRT1_2;
let sq3 = Math.SQRT2;
| 属性 | 描述 |
|---|
| E | 返回欧拉数(约 2.718) |
| LN2 | 返回 2 的自然对数(约 0.693) |
| LN10 | 返回 10 的自然对数(约 2.302) |
| LOG2E | 返回 E 的以 2 为底的对数(约 1.442) |
| LOG10E | 返回 E 的以 10 为底的对数(约 0.434) |
| PI | 返回 PI(约 3.14) |
| SQRT1_2 | 返回 1/2 的平方根(约 0.707) |
| SQRT2 | 返回 2 的平方根(约 1.414) |
Math 对象方法
let n = Math.max(1, 2, 3, 4, 5);
let n = Math.min(1, 2, 3, 4, 5);
let n = Math.trunc(4.5);
let n = Math.ceil(4.1);
let n = Math.floor(4.6);
let n = Math.round(4.1);
let n = Math.round(4.6);
let n = Math.random();
let n = Math.pow(2, 3);
let n = Math.abs(-3);
let n = Math.sqrt(16);
let n = Math.cbrt(27);
let n = Math.sign(5);
let n = Math.sign(0);
let n = Math.sign(-6);
| 方法 | 描述 |
|---|
| max(x, y, z, ..., n) | 最大值 |
| min(x, y, z, ..., n) | 最小值 |
| trunc(x) | 取整 |
| ceil(x) | 向上舍入为最接近的整数 |
| floor(x) | 向下舍入为最接近的整数 |
| round(x) | 将 x 舍入为最接近的整数 |
| random() | 0 到 1 之间的随机数 |
| pow(x, y) | x 的 y 次方 |
| abs(x) | x 的绝对值 |
| sqrt(x) | 开平方 |
| cbrt(x) | 开立方 |
| sign(x) | 返回数的符号(正数:1、负数:-1、零:0) |
| clz32(x) | 返回 x 的 32 位二进制表示中前导零的数量 |
| fround(x) | 数的最接近的(32 位单精度)浮点表示 |
| exp(x) | Ex 的值 |
| expm1(x) | Ex 减去 1 的值 |
| log(x) | x 的自然对数 |
| log2(x) | x 的以 2 为底的对数 |
| log10(x) | x 的以 10 为底的对数 |
| log1p(x) | 1 + x 的自然对数 |
| sin(x) | x 的正弦值(x 以弧度为单位) |
| sinh(x) | x 的双曲正弦值 |
| asin(x) | x 的反正弦值,以弧度为单位 |
| asinh(x) | x 的双曲反正弦值 |
| cos(x) | x 的余弦值(x 以弧度为单位) |
| cosh(x) | x 的双曲余弦值 |
| acos(x) | x 的反余弦值,以弧度为单位 |
| acosh(x) | x 的双曲反余弦值 |
| tan(x) | 数的正切值 |
| tanh(x) | 数的双曲正切值 |
| atan(x) | 返回 x 的反正切值,值是 -PI/2 到 PI/2 之间的弧度值 |
| atan2(y, x) | 返回其参数商的反正切值 |
| atanh(x) | 返回 x 的双曲反正切值 |
数字
Number 对象属性
let n = Number.NEGATIVE_INFINITY;
let n = Number.POSITIVE_INFINITY;
let n = Number.MAX_SAFE_INTEGER;
let n = Number.MIN_SAFE_INTEGER;
let n = Number.MAX_VALUE;
let n = Number.MIN_VALUE;
let n = Number.NaN;
| 属性 | 描述 |
|---|
| constructor | 返回对创建此对象的 Number 函数的引用 |
| prototype | 原型,允许您向对象添加属性和方法 |
| EPSILON | 返回对创建此对象的 Number 函数的引用 |
| MAX_SAFE_INTEGER | 最大安全整数 9007199254740991 |
| MIN_SAFE_INTEGER | 最小安全整数 -9007199254740991 |
| MAX_VALUE | 最大的数 1.7976931348623157 x 10^308(1.79E+308) |
| MIN_VALUE | 最小的数 5 x 10-324(5E-324) |
| NaN | 非数字值 |
| NEGATIVE_INFINITY | 无穷小,溢出时返回该值 (-Infinity) |
| POSITIVE_INFINITY | 无穷大,溢出时返回该 (Infinity) |
Number 对象方法
let num = 5.56789;
let n = num.toFixed(2);
let n = num.toFixed();
let n = Number.isFinite(123);
let n = Number.isInteger(1);
let n = Number.isNaN(0/0);
let n = Number.isSafeInteger(5);
let num = 5.56789;
let n = num.toExponential(3);
let num = 1000000;
let text = num.toLocaleString();
let num = 13.3714;
let n = num.toPrecision(2);
let n = num.toPrecision(3);
let num = 15;
let text = num.toString();
let n = Number.valueOf();
| 方法 | 描述 |
|---|
| toFixed(x) | 把数字转换为字符串,结果的小数点后有指定位数的数字 |
| isFinite(x) | 是否是有限数 |
| isInteger(x) | 是否为整数 |
| isNaN(x) | 是否为 Number.NaN。 |
| isSafeInteger(x) | 是否为安全整数, (2^53 - 1) 到 -(2^53 - 1) |
| toExponential(x) | 将数字转换为指数计数法 |
| toLocaleString(x) | 把数字转换为字符串,使用本地数字格式顺序 |
| toPrecision(x) | 把数字格式化为指定的长度,会四舍五入 |
| toString() | 把数字转换为字符串 |
| valueOf(x) | 返回数字的原始值(基本数字值) |