Math ceil() Math.floor(),Math.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

Math.abs(x)获得一个数值的绝对值javascript

相关文章
相关标签/搜索