源代码
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>享学课堂</title> </head> <body> <p>toPrecision() 方法返回具有指定长度的数字的字符串:</p> <p id="demo"></p> <script> var x = 9.656; document.getElementById("demo").innerHTML = x.toPrecision() + "<br>" + x.toPrecision(2) + "<br>" + x.toPrecision(4) + "<br>" + x.toPrecision(6); </script> </body> </html>
运行结果