JS对象 向下取整floor() floor() 方法可对一个数进行向下取整。 语法: Math.floor(x)

本文详细介绍了JavaScript中向下取整方法floor()的用法,包括其语法、参数及返回值特点,并通过实例展示了如何对不同数值进行向下取整操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

向下取整floor()

floor() 方法可对一个数进行向下取整。

语法:

Math.floor(x)

参数说明:

注意:返回的是小于或等于x,并且与 x 最接近的整数。

我们将在不同的数字上使用 floor() 方法,代码如下:

<script type="text/javascript">
  document.write(Math.floor(0.8)+ "<br>")
  document.write(Math.floor(6.3)+ "<br>")
  document.write(Math.floor(5)+ "<br>")
  document.write(Math.floor(3.5)+ "<br>")
  document.write(Math.floor(-5.1)+ "<br>")
  document.write(Math.floor(-5.9))
</script>

运行结果:

0
6
5
3
-6
-6

 

 

任务

补充右边编辑器在script标签内,使用floor()方法,计算3.3、-0.1、-9.9、8.9值。


<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Math </title>
<script type="text/javascript">
    document.write(Math.floor(3.3)+"<br/>");
    document.write(Math.floor(-0.1)+"<br/>");
    document.write(Math.floor(-9.9)+"<br/>");
    document.write(Math.floor(8.9));
</script>
</head>
<body>
</body>
</html>




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值