JavaScript如何使用Math floor()方法?示例

2021年3月30日15:59:49 发表评论 839 次浏览

以下是示例Math floor方法。

<script type="text/javascript">
  document.write("Result : " + Math.floor(.89));
</script>

Math floor方法用于将作为参数传递的数字四舍五入到向下取整(即朝较小值)的最接近的整数。

语法如下:

Math.floor(value)

参数:此方法接受上述和以下描述的单个参数:

  • 值:它是要对Math.floor进行测试的值。

返回值:Math.floor()方法返回大于或等于给定数字的最小整数。

以下示例说明了JavaScript中的Mathe floor()方法:

示例1:输入:Math.floor(.89)输出:0

示例2:输入:Math.floor(-89.02)输出:-90

示例3:输入:Math.floor(0)输出:0

上述方法的更多代码如下:

程序1:当传递负数作为参数时。

<script type= "text/javascript" >
          document.write( "Result : " + Math.floor(-89.02));
</script>

输出如下:

Result : -90

程式2:将零作为参数传递时。

<script type= "text/javascript" >
          document.write( "Result : " + Math.floor(0));
</script>

输出如下:

Result : 0

支持的浏览器:

  • 谷歌浏览器
  • IE浏览器
  • 火狐浏览器
  • 歌剧
  • 苹果浏览器

木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: