オブジェクトの値を指定された精度で表した文字列に変換します.
numberObject.toPrecision(num)
以下にtoPrecisionメソッドを使用したサンプルコードを示します.
<html>
<head>
<title>toPrecisionメソッドのサンプルコード</title>
</head>
<body>
<script type="text/javascript" language="javascript">
var num = 3.141519;
document.write(num.toPrecision(1) + "<br />");
document.write(num.toPrecision(2) + "<br />");
document.write(num.toPrecision(3) + "<br />");
document.write(num.toPrecision(4) + "<br />");
</script>
</body>
</html>
サンプルコードの実行結果は以下のようになります.
3 3.1 3.14 3.142
スポンサード リンク