js中Math.floor(),Math.round(),Math.ceil()

 时间:2026-02-17 03:34:16

1、Math.floor() ,Math.round()方法执行的是取整计算,它们得到的是与之最接近的整数

当参数没有的时候,返回你NaN

Math.floor()

NaN

Math.round()

NaN

js中Math.floor(),Math.round(),Math.ceil()

js中Math.floor(),Math.round(),Math.ceil()

2、我们再使用Math.floor()传入其它参数看看,如下所示。

可以看出返回的都是比它小的,最接近它的整数

Math.floor(2.9)

2

Math.floor(-2.9)

-3

Math.floor(-0.5)

-1

Math.floor(-0.9)

-1

Math.floor(0.9)

0

Math.floor(1.9)

1

js中Math.floor(),Math.round(),Math.ceil()

3、Math.round() 方法可把一个数字四舍五入为最接近的数。

可以看出Math.round(),的使用和我们平时使用的四舍五入类似,它返回的是最接近它的整数

Math.round(1.2)

1

Math.round(1.6)

2

Math.round(-1.6)

-2

Math.round(0.9)

1

Math.round(0.49)

0

Math.round(0.01)

0

Math.round(-0.01)

-0

Math.round(-0.4)

-0

js中Math.floor(),Math.round(),Math.ceil()

4、ceil() 方法是向上取整计算,它返回的是大于或等于参数的整数数据

同样Math.ceil()没传入参数也是返回NaN

Math.ceil()

NaN

js中Math.floor(),Math.round(),Math.ceil()

5、ceil() 方法是向上取整计算,它和floor其实刚好相反,下面我们传入一些参数来看看该方法,可以看出返回的都是比它大的,最接近它的整数

Math.ceil()

NaN

Math.ceil(1.2)

2

Math.ceil(-1.2)

-1

Math.ceil(22.10)

23

Math.ceil(77.10)

78

Math.ceil(76.9999)

77

Math.ceil(-76.9999)

-76

js中Math.floor(),Math.round(),Math.ceil()

  • 如何使用PPT做出闪电动画效果
  • 【网络画板】不同样式的格子阵动画
  • Flash中如何用按钮控制图形运动
  • flash 取景框制作
  • 如何利用PPT设置碎裂文字效果
  • 热门搜索
    普拉提腹部减肥 玛缇瓷砖怎么样 冬季黄山旅游攻略 怎么教狗狗握手 休闲西装搭配 正定古城游玩攻略 长城m4怎么样 群等级怎么升 局部减肥 嗓子痒咳嗽怎么回事