JavaScript/Number:修订间差异
来自康健生活
小 文字替换 -“{{leftrightflex|”替换为“{{Double-columns|” |
|||
| (未显示同一用户的1个中间版本) | |||
| 第1行: | 第1行: | ||
== 引言 == | == 引言 == | ||
在内部被表示为 64 位的浮点数。且没有分离整数类型。 | 在内部被表示为 64 位的浮点数。且没有分离整数类型。 | ||
{{ | {{Double-columns|<syntaxhighlight lang="javascript"> | ||
1 === 1.0</syntaxhighlight>|{{consoleout|<pre> | 1 === 1.0</syntaxhighlight>|{{consoleout|<pre> | ||
true | true | ||
| 第10行: | 第10行: | ||
== 指数 == | == 指数 == | ||
{{ | {{Double-columns|<syntaxhighlight lang="javascript"> | ||
console.log(Number.isFinite(10), Number.isFinite(-Infinity), Number.isFinite('10')); // 检测数字是否为有限数字 | console.log(Number.isFinite(10), Number.isFinite(-Infinity), Number.isFinite('10')); // 检测数字是否为有限数字 | ||
console.log(Number.isNaN(10), Number.isNaN(NaN), Number.isNaN('10')); // 检测是否为 NaN | console.log(Number.isNaN(10), Number.isNaN(NaN), Number.isNaN('10')); // 检测是否为 NaN | ||
| 第29行: | 第29行: | ||
== 四舍五入 == | == 四舍五入 == | ||
=== Math.round === | === Math.round === | ||
{{ | {{Double-columns|<syntaxhighlight lang="javascript"> | ||
Math.round(55.4355 * 100) / 100;</syntaxhighlight>|{{consoleout|<pre> | Math.round(55.4355 * 100) / 100;</syntaxhighlight>|{{consoleout|<pre> | ||
55.44 | 55.44 | ||
| 第35行: | 第35行: | ||
=== toFixed === | === toFixed === | ||
舍入规则 Number.prototype.toFixed ( fractionDigits )<ref>Ecma International.[https://262.ecma-international.org/6.0/#sec-number.prototype.tofixed ECMAScript® 2015 Language Specification]:Number.prototype.toFixed ( fractionDigits )[S]:2015</ref>。 | 舍入规则 Number.prototype.toFixed ( fractionDigits )<ref>Ecma International.[https://262.ecma-international.org/6.0/#sec-number.prototype.tofixed ECMAScript® 2015 Language Specification]:Number.prototype.toFixed ( fractionDigits )[S]:2015</ref>。 | ||
{{ | {{Double-columns|<syntaxhighlight lang="javascript"> | ||
(new Number(100.345)); | (new Number(100.345)); | ||
(new Number(100.345)).toFixed(2);</syntaxhighlight>|{{consoleout|<pre> | (new Number(100.345)).toFixed(2);</syntaxhighlight>|{{consoleout|<pre> | ||
| 第43行: | 第43行: | ||
=== 自定义 === | === 自定义 === | ||
{{ | {{Double-columns|<syntaxhighlight lang="javascript"> | ||
function fixed(num, digit) { | function fixed(num, digit) { | ||
if(Object.is(parseFloat(num), NaN)) { | if(Object.is(parseFloat(num), NaN)) { | ||
| 第60行: | 第60行: | ||
| kind-of || is-number || big.js | | kind-of || is-number || big.js | ||
|- | |- | ||
| | | bignumber.js || decimal.js || 示例 | ||
|- | |- | ||
| 示例 || 示例 || 示例 | | 示例 || 示例 || 示例 | ||
|} | |} | ||