zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    Eclipse 中生成帮助文档 (javadoc) 迎客
    网管利器:七大免费网络工具 迎客
    oracle 11g 学习笔记 10_27
    oracle 11g 学习笔记 10_29
    oracle 11g学习笔记 2012_10_22
    oracle 11g 学习笔记 2012_10_25(2)
    oracle 11g 学习笔记 10_26
    oracle 11g 学习笔记 2012_10_24(1)
    oracle 11g 学习笔记2012_10_23(2)
    oracle 11g 学习笔记 2012_10_25(a)
  • 原文地址:https://www.cnblogs.com/Jacklovely/p/5279808.html
Copyright © 2011-2022 走看看