JavaScript中的Number.toFixed()函数
toFixed()
Number对象的功能接受一个数字,该数字表示要在小数点后显示的点数,并相应地显示当前数字。
语法
其语法如下
num.toFixed(num);
示例
<html> <head> <title>JavaScript Example</title> </head> <body> <script type="text/javascript"> var num = Math.PI; result = num.toFixed(4); document.write("Fixed point notation of the given number is: " + result); </script> </body> </html>
输出结果
Fixed point notation of the given number is: 3.1416
示例
<html> <head> <title>JavaScript Example</title> </head> <body> <script type="text/javascript"> var num = Math.PI; document.write("Fixed point notation of the given number is: " + num.toFixed(4)); document.write("<br>"); var num = 2.13e+15; document.write("Fixed point notation of the given number is: " + num.toFixed(4)); </script> </body> </html>
输出结果
Fixed point notation of the given number is: 3.1416 Fixed point notation of the given number is: 2130000000000000.0000