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