JavaScript/Number:修订间差异
来自康健生活
| 第1行: | 第1行: | ||
== 引言 == | == 引言 == | ||
{{leftrightflex|<syntaxhighlight lang="javascript"> | |||
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.parseInt(10.123), Number.parseInt(void 1), Number.parseInt('10.456')); // 返回整数 | |||
console.log(Number.parseFloat('123.45#abc'), Number.parseFloat(void 1), Number.parseFloat('abc123.45')); // 返回浮点数 | |||
console.log(Number.isInteger(10), Number.isInteger(10.0), Number.isInteger('10.0')); // 判断是否为整数 | |||
console.log(Math.trunc('123.456')); // 去掉小数 | |||
console.log(Math.sign('10'), Math.sign(-10), Math.sign(0), Math.sign('-0'), Math.sign(void 1)); // 判断数字类型</syntaxhighlight>|{{consoleout|<pre> | |||
true false false | |||
false true false | |||
10 NaN 10 | |||
123.45 NaN NaN | |||
true true false | |||
123 | |||
1 -1 0 -0 NaN | |||
</pre>}}}} | |||
== 四舍五入 == | == 四舍五入 == | ||
=== Math.round === | === Math.round === | ||
2022年3月11日 (五) 08:31的版本
引言
四舍五入
Math.round
toFixed
舍入规则 Number.prototype.toFixed ( fractionDigits )[1]。 模板:Leftrightflex
自定义
- ↑ Ecma International.ECMAScript® 2015 Language Specification:Number.prototype.toFixed ( fractionDigits )[S]:2015
No categories