js之上舍入、下舍入、四舍五入等

小鱼儿 2023-01-13 06:11 323阅读 0赞

上舍入 Math.ceil()

  1. Math.ceil(1.2345)
  2. 2
  3. Math.ceil(1.2345*100) / 100
  4. 1.24

下舍入 Math.floor

  1. Math.floor(1.2345)
  2. 1
  3. Math.floor(1.2345*100) / 100
  4. 1.23

四舍五入 Math.round

  1. Math.round(1.2345)
  2. 1
  3. Math.round(1.5345)
  4. 2

只保留整数部分(丢弃小数部分) parseInt

  1. parseInt(1.2345)
  2. 1
  3. parseInt(1.7345)
  4. 1

取绝对值 Math.abs

  1. Math.abs(-1)
  2. 1

返回两数中的较大者 Math.max

  1. Math.max(1,2)
  2. 2

返回两数中的较小者 Math.min

  1. Math.min(1,2)
  2. 1

随机数(0-1)返回 0(包括) 至 1(不包括) 之间的随机数 Math.random

  1. Math.random()
  2. 0.05559749299436745

随机数(0-9) Math.floor(Math.random() * 10)

随机数(0-99) Math.floor(Math.random() * 100)

随机数(1-10) Math.floor(Math.random() * 10) + 1

随机数(1-100) Math.floor(Math.random() * 100) + 1

发表评论

表情:
评论列表 (有 0 条评论,323人围观)

还没有评论,来说两句吧...

相关阅读

    相关 银行家

           我们在小学就学过四舍五入的方法,但是四舍五入并非真正的公平,0其实本身没有意义,所以1-4共四个数舍去,5-9五个数进一位。其实并非真正的公平精确,所以在银行业就

    相关

    四舍五入 这种方法小学的时候就学过,它虽然简单,但是产生误差累计的因素还是很明显的。 就拿保留整数来说: 小数部分从0.0~0.4999,是属于“舍”的范围,产生的