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 ...
随机推荐
- java的过滤器对session进行检查
private void sendErrorToBrowser(HttpServletRequest request,HttpServletResponse response) throws IOEx ...
- python之函数用法vars()
# -*- coding: utf-8 -*- #python 27 #xiaodeng #python之函数用法vars() #vars() #说明:返回对象object的属性和属性值的字典对象 ' ...
- 【redis】常用命令
三.常用命令 1)连接操作命令 quit:关闭连接(connection) auth:简单密码认证 help cmd: 查看cmd帮助,例如:help quit ...
- ASP.NET#JavaScript中调用WebServer注意的问题
1)要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,就要取消注销以下行:[System.Web.Script.Services.ScriptService](2)调用格式:命名空间 ...
- springmvc多视图配置
http://blog.csdn.net/yaerfeng/article/details/23593755
- HDUOJ----Good Numbers
Good Numbers Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tot ...
- Spring MVC+Mybatis 执行存储过程,使用Map进行参数的传递
研究了一天mybatis如何执行存储过程,基本了解了ORM的设计思想,在map层面进行对象关系映射有两种思路. 根据不同的业务使用不同的思路: 一.实体类和数据库映射,就是将数据库中的字段和java实 ...
- 【Linux】在线求助man page与info page
先来了解一下Linux有多少命令呢?在文本模式下,你可以直接按下两个[Tab]按键,看看总共有多少命令可以让你用? [knife@www ~]$ <==在这里不要输入任何字符,直接输入两次[ta ...
- NGUI: UIPanel控件
转自:http://blog.csdn.net/huang9012/article/details/19411703 http://bbs.9ria.com/thread-417659-1-1.htm ...
- Java 发送邮件工具包---未完待续
开头先来介绍一片好文章: http://blog.itpub.net/15182208/viewspace-730172/