JavaScript 中的 Math.round() 函数
Math 对象的 round() 函数接受一个浮点数随机数并返回其最接近的整数。
- 如果给定的数字是 x.5 或更大,此函数将返回下一个数字 (x + 1)
- 如果给定数字是 x.4 或更小,此函数将返回上一个数字 (x - 1)
- 如果给定数字本身是一个整数,此函数将返回相同的值。
语法
其语法如下
Math.round();
示例
<html>
<head>
<title>JavaScript Example</title>
</head>
<body>
<script type="text/javascript">
var result = Math.round(2541.542);
document.write("Rounded value of the given number: "+result);
</script>
</body>
</html>输出
Rounded value of the given number: 2542
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 程序设计
C++
C#
MongoDB
MySQL
Javascript
PHP