[摘要:之前常常正在代码中看到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小数取整小结【转】的更多相关文章

  1. js中Math.round、parseInt、Math.floor和Math.ceil小数取整小结

    以前经常在代码中看到Math.round.parseInt.Math.floor和Math.ceil这四个函数,虽然知道结果都可以返回一个整数,但是对他们四者的区别还是不太清楚,今天就做一个小结. 一 ...

  2. js中Math.round、parseInt、Math.floor和Math.ceil小数取整总结(转)

    js中Math.round.parseInt.Math.floor和Math.ceil小数取整总结 Math.round.parseInt.Math.floor和Math.ceil 都可以返回一个整数 ...

  3. js中Math.round、parseInt、Math.floor和Math.ceil小数取整总结

    Math.round.parseInt.Math.floor和Math.ceil 都可以返回一个整数,具体的区别请看下面的总结. 一.Math.round 作用:四舍五入,返回参数+0.5后,向下取整 ...

  4. js缓慢运动,Math.ceil向上取整,floor向下取整

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  5. 【转载】C#使用Math.Floor方法来向下取整

    在C#的数值运算中,有时候需要对计算结果舍去小数位保留整数位向下取整即可,此时就可使用内置方法Math.Floor来实现向下取整操作,Math.Floor方法将舍去小数部分,保留整数.Math.Flo ...

  6. js中对小数取整

    js中对小数取整的函数,需要的朋友可以参考下.   1.丢弃小数部分,保留整数部分 js:parseInt(7/2) 2.向上取整,有小数就整数部分加1 js: Math.ceil(7/2) 3,四舍 ...

  7. JS你所不知的小数取整方法

    先介绍几种基本方法. 1.toFixed()方法 toFixed() 方法是属于 Number 对象的方法,可以把 Number 四舍五入到指定的小数位数,括号内为小数位数,范围为0~20,为0时即取 ...

  8. javaScript中小数取整,四种方法的比较

    1.parseInt:只取整数位例如:parseInt(3.7) 取整结果为:3parseInt(-1.1) 取整结果为:-1 2.Math.floor :向下去整,取大的整数例如:Math.floo ...

  9. js 常见的小数取整问题

    1.四舍五入取整 Math.round(5/2)   // 3 2.直接去掉小数,取整 parseInt(5/2);     //  2 3.向上取整,有小数整数部分就加1 Math.ceil(1/3 ...

随机推荐

  1. 一段测试try...catch运行时间的代码

    public class Test2 { private static int test() {         int i=1; try {             i=2;             ...

  2. Linux-JAVA-JDK完整配置过程

        JDK是目前很流行的开发工具包,内含jre是支撑jvm运行的组件. 01.jdk下载 http://www.oracle.com/technetwork/java/javase/downloa ...

  3. ZooKeeper做独立server执行(上)

    ZooKeeper做独立server执行(上) 作者:chszs.转载需注明.博客主页:http://blog.csdn.net/chszs 一.ZooKeeper安装及配置 版本号:ZooKeepe ...

  4. Android 5.0 + IDA 6.8 调试经验分享

    如今升级快.网上的资料仅仅能做參考. 学到了NDK逆向这一块,昨天为了能让IDA 能动态调试SO,瞎折腾了非常久,这里分享一下我的经验. 工具: IDA pro 6.8 Android 5.x IDA ...

  5. LR杂记 - 性能測试指标及经常使用的监控工具

    监控指标 性能測试通常须要监控的指标包含: 1.serverLinux(包含CPU.Memory.Load.I/O). 2.数据库:1.Mysql 2.Oracle(缓存命中.索引.单条SQL性能.数 ...

  6. android的download manager(1)

    android 2.3中引入了Download manager.作为一个service来优化长时间下载操作的处理.Download Manager通过处理HTTP链接.监控链接的变化和系统又一次启动来 ...

  7. PHP-Open Flash Chart报表生成

    下载: http://www.cnblogs.com/huangcong/archive/2013/01/27/2878650.html 安装: 解压ZIP包, 将open-flash-chart.s ...

  8. 用setitimer实现多个定时器

    从这篇文章中可以看出,setitimer只能实现一个定时器,如果多次调用setitimer,旧值都会被覆盖掉. 如何用setitimer实现多个定时器呢?下面是我的一个实现,具体的方法是: 用链表从小 ...

  9. Fork me on GitHub

    <a href="https://github.com/yadongliang"><img style="position: absolute; top ...

  10. cxf之org.xml.sax.SAXParseException; lineNumber: 7; columnNumber: 37; cvc-complex-type.2.4.c: 通配符的匹配很全面, 但无法找到元素 'jaxws:server' 的声明。

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...