toFixed() 方法将数字四舍五入为指定位的数字
- <!DOCTYPE html>
- <html>
- <body>
- <h1>JavaScript 数字方法</h1>
- <p>toFixed() 方法将数字四舍五入为给定的位数。</p>
- <p>对于处理金钱,toFixed(2) 是完美的。</p>
- <p id="demo"></p>
- <script>
- var x = 9.656;
- document.getElementById("demo").innerHTML =
- x.toFixed(0) + "<br>" +
- x.toFixed(2) + "<br>" +
- x.toFixed(4) + "<br>" +
- x.toFixed(6);
- </script>
- </body>
- </html>