js中Math.round、parseInt、Math.floor和Math.ceil小数取整小结【转】
[摘要:之前常常正在代码中看到Math.round、parseInt、Math.floor战Math.ceil那四个函数,固然晓得效果皆能够返回一个整数,然则对他们四者的差别照样没有太清晰,本日便做一个小结。 1、Math.round 感化]
以前经常在代码中看到Math.round、parseInt、Math.floor和Math.ceil这四个函数,虽然知道结果都可以返回一个整数,但是对他们四者的区别还是不太清楚,今天就做一个小结。
一、Math.round
作用:四舍五入,返回参数+0.5后,向下取整。
如:
Math.round(5.57) //返回6
Math.round(2.4) //返回2
Math.round(-1.5) //返回-1
Math.round(-5.8) //返回-6
二、parseInt
作用:解析一个字符串,并返回一个整数,这里可以简单理解成返回舍去参数的小数部分后的整数。
如:
parseInt(5.57) //返回5
parseInt(2.4) //返回2
parseInt(-1.5) //返回-1
parseInt(-5.8) //返回-5
三、Math.floor
作用:返回小于等于参数的最大整数。
如:
Math.floor(5.57) //返回5
Math.floor(2.4) //返回2
Math.floor(-1.5) //返回-2
Math.floor(-5.8) //返回-6
四、Math.ceil
作用:返回大于等于参数的最小整数
Math.ceil(5.57) //返回6
Math.ceil(2.4) //返回3
Math.ceil(-1.5) //返回-1
Math.ceil(-5.8) //返回-5
原文:http://www.ithao123.cn/content-6071284.html
js中Math.round、parseInt、Math.floor和Math.ceil小数取整小结【转】的更多相关文章
- js中Math.round、parseInt、Math.floor和Math.ceil小数取整小结
以前经常在代码中看到Math.round.parseInt.Math.floor和Math.ceil这四个函数,虽然知道结果都可以返回一个整数,但是对他们四者的区别还是不太清楚,今天就做一个小结. 一 ...
- js中Math.round、parseInt、Math.floor和Math.ceil小数取整总结(转)
js中Math.round.parseInt.Math.floor和Math.ceil小数取整总结 Math.round.parseInt.Math.floor和Math.ceil 都可以返回一个整数 ...
- js中Math.round、parseInt、Math.floor和Math.ceil小数取整总结
Math.round.parseInt.Math.floor和Math.ceil 都可以返回一个整数,具体的区别请看下面的总结. 一.Math.round 作用:四舍五入,返回参数+0.5后,向下取整 ...
- js缓慢运动,Math.ceil向上取整,floor向下取整
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- 【转载】C#使用Math.Floor方法来向下取整
在C#的数值运算中,有时候需要对计算结果舍去小数位保留整数位向下取整即可,此时就可使用内置方法Math.Floor来实现向下取整操作,Math.Floor方法将舍去小数部分,保留整数.Math.Flo ...
- js中对小数取整
js中对小数取整的函数,需要的朋友可以参考下. 1.丢弃小数部分,保留整数部分 js:parseInt(7/2) 2.向上取整,有小数就整数部分加1 js: Math.ceil(7/2) 3,四舍 ...
- JS你所不知的小数取整方法
先介绍几种基本方法. 1.toFixed()方法 toFixed() 方法是属于 Number 对象的方法,可以把 Number 四舍五入到指定的小数位数,括号内为小数位数,范围为0~20,为0时即取 ...
- javaScript中小数取整,四种方法的比较
1.parseInt:只取整数位例如:parseInt(3.7) 取整结果为:3parseInt(-1.1) 取整结果为:-1 2.Math.floor :向下去整,取大的整数例如:Math.floo ...
- js 常见的小数取整问题
1.四舍五入取整 Math.round(5/2) // 3 2.直接去掉小数,取整 parseInt(5/2); // 2 3.向上取整,有小数整数部分就加1 Math.ceil(1/3 ...
随机推荐
- vs2010支持html5+css3
第一步. 先到微软官方下载一个 Microsoft Visual Studio 2010 sp1 . 给传送门:下载 下载到这个东东 ---
- 修改wamp apache 默认端口
目标:新配置一个虚拟主机,要求端口为8080,但要保证原来的80端口仍可用 1.修改 httpd.conf 开启vhost文件支持 2.httpd-vhosts.conf 中增加,记得把原来的内容全部 ...
- 搭建前端vue环境,安装vue-cli遇到Please try running this command again as root/Administrator的解决方案
最近在搭前端环境,装完node.js之后,准备安装vue工程的初始化工具时(npm install -g vue-cli),遇到这个坑: 大体的意思就是权限问题,导致/usr/local/lib/no ...
- HDUOJ-------2844Coins
Coins Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submi ...
- linux命令中的 < 和 |、>符号作用就解释
输出重定向比如输入一条命令,默认行为是将结果输出到屏幕.但有时候我们需要将输出的结果保存到文件,就可以用重定向.ps > ps.txt < 表示的是输入重定向的意思,就是把<后面跟的 ...
- CentOS6.6 32位 Minimal版本纯编译安装Nginx Mysql PHP Memcached
声明:部分编译指令在博客编辑器里好像被处理了,如双横线变成单横线了等等,于是在本地生成了一个pdf版本,在下面地址可以下载. LNMP+Memcached CentOS是红帽发行的免费的稳定Linux ...
- 配置tomcat全局c3p0连接池
由于项目中多个应用访问同一个数据库,并部署在同一个tomcat下面,所以没必要每个应用都配置连接池信息,这样可能导致数据库的资源分布不均,所以这种情况完全可以配置一个tomcat的全局连接池,所涉及应 ...
- [Android实例] 拖动滑块进行图片拼合验证方式的实现
该篇文章从eoeAndroid搬迁过来的,原文地址:[Android实例] 拖动滑块进行图片拼合验证方式的实现 现在网站上有各种各样的验证码验证方式,比如计算大小,输入图片内容等,今天在一家网站上看到 ...
- mysql 监控qps脚本
mysqladmin -h172.16.200.43 -uroot -p status -i 1 #!/bin/bash mysqladmin -uroot -h172.16.200.43 -p'00 ...
- [转]VTH changes in DC from Hspice
Hello, everyone. I’d like to know the threshold of the MOS transistor. And I found the “.print vth() ...