四舍五入round()

round() 方法可把一个数字四舍五入为最接近的整数。

语法:

Math.round(x)

参数说明:

注意:

1. 返回与 x 最接近的整数。

2. 对于 0.5,该方法将进行上舍入。(5.5 将舍入为 6)

3. 如果 x 与两侧整数同等接近,则结果接近 +∞方向的数字值 。(如 -5.5 将舍入为 -5; -5.52 将舍入为 -6),如下图:

把不同的数舍入为最接近的整数,代码如下:

<script type="text/javascript">
document.write(Math.round(1.6)+ "<br>");
document.write(Math.round(2.5)+ "<br>");
document.write(Math.round(0.49)+ "<br>");
document.write(Math.round(-6.4)+ "<br>");
document.write(Math.round(-6.6));
</script>

运行结果:

2
3
0
-6
-7

任务

补充右边编辑器代码,在script标签内,使用round()方法,计算3.3、-0.1、-9.9、8.9值。

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Math </title>
<script type="text/javascript">
document.write(Math.round(3.3)+"<br/>");
document.write(Math.round(-0.1)+"<br/>");
document.write(Math.round(-9.9)+"<br/>");
document.write(Math.round(8.9));
</script>
</head>
<body>
</body>
</html>

最新文章

  1. VS2010 release编译下进行调试,“当前不会命中任何断点,还没有为文档加载”问题解决方案
  2. mysql中生产表格多列统计问题
  3. 用arp-scan扫描局域网IP地址
  4. Mysql 全文索引
  5. 一个Json结构对比的Python小工具兼谈编程求解问题
  6. vi编辑器基本用法介绍
  7. POJ 1067 取石子游戏(威佐夫博弈)
  8. xmlHTTP技术资料
  9. JAVA控制台输入输出方法总结
  10. 使用nat方式解决虚拟机联网问题
  11. BZOJ 4727: [POI2017]Turysta
  12. javascript event 事件解析
  13. 2.docker的网络模式
  14. 求集合中选一个数与当前值进行位运算的max
  15. Spring Boot 常见标签
  16. Ubuntu16.04安装8821CE 无线网卡无驱动
  17. angular组件之间的通讯
  18. TCP的状态转移
  19. list页面-按照choice筛选丶传condition过滤筛选项丶筛选与显示同步
  20. 基于ONVIF协议的摄像头开发总结

热门文章

  1. 并发编程(六)——进程/线程池、协程、gevent第三方库
  2. 第二记 Java数据类型
  3. python颜色
  4. wx.request 小程序之数据请求
  5. Django项目 BBS论坛
  6. Ubuntu 更新国内镜像源失败
  7. NFS 服务器的配置
  8. NCM格式转换MP3格式
  9. Hadoop&ndash;Task 相关
  10. sqlmap 使用方法及实例