zoukankan      html  css  js  c++  java
  • Javascript -- Math.round()、Math.ceil()、Math.floor()、parseInt去小数取整总结

    一、Math.round()

    作用:四舍五入返回整数。(返回参数+0.5后,向下取整)

    Math.round(5.57)  //返回6

    Math.round(2.4)   //返回2

    Math.round(-1.5)  //返回-1

    Math.round(-5.8)  //返回-6

      

    二、Math.ceil()

    作用:返回大于等于参数的最小整数。

    Math.ceil(5.57)  //返回6

    Math.ceil(2.4)  //返回3

    Math.ceil(-1.5)  //返回-1

    Math.ceil(-5.8)  //返回-5

     

    三、Math.floor()

    作用:返回小于等于参数的最大整数。 

    Math.floor(5.57)  //返回5

    Math.floor(2.4)  //返回2

    Math.floor(-1.5)  //返回-2

    Math.floor(-5.8)  //返回-6

     

    四、parseInt()

    作用:解析一个字符串,并返回一个整数,这里可以简单理解成返回舍去参数的小数部分后的整数。

    parseInt(5.57)  //返回5

    parseInt(2.4)  //返回2

    parseInt(-1.5)  //返回-1

    parseInt(-5.8)  //返回-5

    正数转换和Math.floor()一样,负数不一样

    五、 Math.random()

    该方法可返回介于 0 ~ 1 之间的一个随机数

    如果你希望生成任意值到任意值的随机数,公式就是这样的:

    // max - 期望的最大值, min - 期望的最小值 

    parseInt(Math.random()*(max-min+1)+min,10);
    Math.floor(Math.random()*(max-min+1)+min);

  • 相关阅读:
    1、常见ELK架构工作流程
    centos7系统zabbix 4.4版本升级到5.0版本
    K3s简介(一)
    三、saltstack数据系统grains
    爬取猫眼电影top100信息
    第一次爬虫实例
    docker容器轻量级web管理工具之portainer(六)
    liunx添加swap分区
    iptables 配置详解
    几个比较经典的算法问题的java实现
  • 原文地址:https://www.cnblogs.com/fhen/p/6210381.html
Copyright © 2011-2022 走看看