分享

math.floor() math.ceil() math.round()

 昵称18349714 2014-08-28

Math.floor(x)

功能:对一个数进行下取整。

语法: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.ceil(x)

功能:对一个数进行上取整。

语法: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.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


    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多