查看“︁JavaScript/Number”︁的源代码
来自康健生活
←
JavaScript/Number
因为以下原因,您没有权限编辑该页面:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
== 引言 == == 四舍五入 == === Math.round === {{leftrightflex|<syntaxhighlight lang="javascript"> Math.round(55.4355 * 100) / 100;</syntaxhighlight>|{{consoleout|<pre> 55.44 </pre>}}}} === toFixed === {{leftrightflex|<syntaxhighlight lang="javascript"> (new Number(100.345)); (new Number(100.345)).toFixed(2);</syntaxhighlight>|{{consoleout|<pre> Number {100.345} '100.34' </pre>}}}} === 自定义 === {{leftrightflex|<syntaxhighlight lang="javascript"> function fixed(num, digit) { if(Object.is(parseFloat(num), NaN)) { return console.log(`传入的值:${num}不是一个数字`); } num = parseFloat(num); return (Math.round((num + Number.EPSILON) * Math.pow(10, digit)) / Math.pow(10, digit)).toFixed(digit); } fixed(12.34567, 4);</syntaxhighlight>|{{consoleout|<pre> '12.3457' </pre>}}}}
此页面嵌入的页面:
模板:Consoleout
(
查看源代码
)
模板:Double-columns
(
查看源代码
)
模板:Double-columns/style.css
(
查看源代码
)
返回
JavaScript/Number
。
导航菜单
康健生活
搜索
user-page
通知
user-interface-preferences
个人工具
登录
五一
多读书,多运动,多到远处走走,再看看不一样的世界。