最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 科技 - 知识百科 - 正文

javascript对象之内置对象Math使用方法_基础知识

来源:动视网 责编:小采 时间:2020-11-27 20:48:49
文档

javascript对象之内置对象Math使用方法_基础知识

javascript对象之内置对象Math使用方法_基础知识:一、Math.min()和Math.max(),分别返回参数中的最小和最大值 例: alert(Math.min(1,2,3)) //输出 1 alert(Math.max(1,2,3)) //输出 3 二、Math.abs(),返回参数的绝对值 例: alert(Math.abs(-1)) //输出 1 三、
推荐度:
导读javascript对象之内置对象Math使用方法_基础知识:一、Math.min()和Math.max(),分别返回参数中的最小和最大值 例: alert(Math.min(1,2,3)) //输出 1 alert(Math.max(1,2,3)) //输出 3 二、Math.abs(),返回参数的绝对值 例: alert(Math.abs(-1)) //输出 1 三、


一、Math.min()和Math.max(),分别返回参数中的最小和最大值
  例:
  alert(Math.min(1,2,3))  //输出 "1"
  alert(Math.max(1,2,3))  //输出 "3"

二、Math.abs(),返回参数的绝对值
  例:
  alert(Math.abs(-1))  //输出 "1"

三、Math.random(),产生一个0到1的随机数
  例:
  window.open("http://www.***.com/index.shtml?t="+Math.random)  //在url地址后面加上一个值为随即数的参数,能保证页面每次都从服务器上重新拉取,而不是读取缓存。

四、Math.floor(),Math.round(),Math.ceil()
  Math.floor():把小数向下舍入成整数    例:alert(Math.floor(1.5))  //输出"1"
  Math.round():把小数标准四舍五入成整数  例:alert(Math.round(1.5))  //输出"2"
  Math.ceil():把小数向上舍入成整数  例:alert(Math.round(1.5))  //输出"2"
  利用这三个函数,在涉及小数计算的时候就非常方便,比如设计如下函数来进行小数处理
代码如下:
  function test(num,flag,bit)  //参数分别是 要传入的小数"num"   舍入标准(-1,向下;0,标准;1向上)"flag"  保留小数的位数"bit"
  {
    var n=Math.pow(10,bit);
    switch(flag)
    {
      case -1:return Math.floor(num*n)/n;break;
      case 0:return Math.round(num*n)/n;break;
      case 1:return Math.ceil(num*n)/n;
    }
  }

文档

javascript对象之内置对象Math使用方法_基础知识

javascript对象之内置对象Math使用方法_基础知识:一、Math.min()和Math.max(),分别返回参数中的最小和最大值 例: alert(Math.min(1,2,3)) //输出 1 alert(Math.max(1,2,3)) //输出 3 二、Math.abs(),返回参数的绝对值 例: alert(Math.abs(-1)) //输出 1 三、
推荐度:
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top