四捨五入を行います.
Math.round(x)
roundメソッドはxを四捨五入し,最も近似の整数を返します.
以下にroundメソッドを使用したサンプルコードを示します.
<html>
<head>
<title>roundメソッドのサンプルコード</title>
</head>
<body>
<script type="text/javascript" language="javascript">
document.write(Math.round(1.0001) + "<br />");
document.write(Math.round(-1.0001) + "<br />");
document.write(Math.round(1.5000) + "<br />");
document.write(Math.round(-1.5000) + "<br />");
document.write(Math.round(0) + "<br />");
</script>
</body>
</html>
サンプルコードの実行結果は以下のようになります.
1 -1 2 -1 0
スポンサード リンク