首页 > 编程 > JS > 正文

Javascript中Math.round方法的使用

2023-04-13 12:29:58
字体:
来源:转载
供稿:网友

JavaScript中的round() 方法是把一个数字舍入为最接近的整数。其语法形式如下:

Math.round(x)

x是数字型的参数,其计算结果为与x最接近的整数。

如,如果x为1.5,则结果为2,如果x为-1.5,则结果为1。

下面再给出几个实例,让大家了解其具体使用方法:

<script type="text/javascript">
document.write(Math.round(0.70) + "<br />")
document.write(Math.round(0.50) + "<br />")
document.write(Math.round(0.48) + "<br />")
document.write(Math.round(1.50) + "<br />")
document.write(Math.round(-1.50) + "<br />")
document.write(Math.round(-3.90) + "<br />")
document.write(Math.round(-3.10))
</script>

运算结果为:

1
1
0
2
-1
-4
-3

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表