Math类中提供了三个与取整有关的方法:ceil,floor,round,这些方法的作用于它们的英文名称的含义相对应

1、ceil的英文意义是天花板,该方法就表示向上取整,Math.ceil(11.3)的结果为12,Math.ceil(-11.6)的结果为-11;

2、floor的英文是地板,该方法就表示向下取整,Math.floor(11.6)的结果是11,Math.floor(-11.4)的结果-12;

3、最难掌握的是round方法,他表示“四舍五入”,算法为Math.floor(x+0.5),即将原来的数字加上0.5后再向下取整,所以,Math.round(11.5)的结果是12,Math.round(-11.5)的结果为-11.

相关文章:

  • 2022-12-23
  • 2021-07-02
  • 2022-12-23
  • 2021-10-07
  • 2021-06-28
  • 2022-12-23
猜你喜欢
  • 2021-11-29
  • 2021-05-20
  • 2022-12-23
  • 2021-12-27
  • 2022-12-23
相关资源
相似解决方案