zoukankan      html  css  js  c++  java
  • Math ceil()、floor()、round()方法

    Math.ceil()
     
    功能:对一个数进行上取整。
    语法:Math.ceil(x)
    参数:
    • x:一个数值。
    返回值:返回大于或等于x,并且与之最接近的整数。
    注:如果x是正数,则把小数“入”;如果x是负数,则把小数“舍”。
    例:
    <script type="text/javascript">
    document.write( Math.ceil(1.2)+", "+Math.ceil(1.8)+", "+Math.ceil(-1.2)+", "+Math.ceil(-1.8) );
    </script>
    输出结果为:
    document.write( Math.ceil(1.2)+", "+Math.ceil(1.8)+", "+Math.ceil(-1.2)+", "+Math.ceil(-1.8) ); 2, 2, -1, -1
     
    Math.floor()
     
    功能:对一个数进行下取整。
    语法:Math.floor(x)
    参数:
    • x:一个数值。
    返回值:返回小于或等于x,并且与之最接近的整数。
    注:如果x是正数,则把小数“舍”;如果x是负数,则把小数“入”。
    例:
    <script type="text/javascript">
    document.write( Math.floor(1.2)+", "+Math.floor(1.8)+", "+Math.floor(-1.2)+", "+Math.floor(-1.8) );
    </script>
    输出结果为:
    document.write( Math.floor(1.2)+", "+Math.floor(1.8)+", "+Math.floor(-1.2)+", "+Math.floor(-1.8) ); 1, 1, -2, -2
     

    Math.round()
     
    功能:四舍五入取整。
    语法:Math.round(x)
    参数:
    • x:一个数值。
    返回值:与x最接近的整数。
    例:
    <script type="text/javascript">
    document.write( Math.round(1.2)+", "+Math.round(1.8)+", "+Math.round(-1.2)+", "+Math.round(-1.8) );
    </script>
    输出结果为:
    document.write( Math.round(1.2)+", "+Math.round(1.8)+", "+Math.round(-1.2)+", "+Math.round(-1.8) ); 1, 2, -1, -2
  • 相关阅读:
    Ubuntu16.04上安装搜狗输入法
    RAM的分类
    解耦技巧——依赖注入!
    傻瓜式学Python3——列表
    Java Builder 模式,你搞懂了么?
    String 源码浅析(一)
    线上CPU100%?看看这篇是怎么排查的!
    生物医学工程SCI期刊投稿(转)
    免费下载文献
    泰山攻略
  • 原文地址:https://www.cnblogs.com/warmth89/p/4343679.html
Copyright © 2011-2022 走看看