源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

javascript对象之内置对象Math使用方法

  • 时间:2020-09-16 14:31 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:javascript对象之内置对象Math使用方法
[b]一、Math.min()和Math.max(),分别返回参数中的最小和最大值[/b]   例:   alert(Math.min(1,2,3))  //输出 "1"   alert(Math.max(1,2,3))  //输出 "3" [b]二、Math.abs(),返回参数的绝对值[/b]   例:   alert(Math.abs(-1))  //输出 "1" [b]三、Math.random(),产生一个0到1的随机数[/b]   例:   window.open("http://www.***.com/index.shtml?t="+Math.random)  //在url地址后面加上一个值为随即数的参数,能保证页面每次都从服务器上重新拉取,而不是读取缓存。 [b]四、Math.floor(),Math.round(),Math.ceil()[/b]   Math.floor():把小数向下舍入成整数    例:alert(Math.floor(1.5))  //输出"1"   Math.round():把小数标准四舍五入成整数  例:alert(Math.round(1.5))  //输出"2"   Math.ceil():把小数向上舍入成整数  例:alert(Math.round(1.5))  //输出"2"   利用这三个函数,在涉及小数计算的时候就非常方便,比如设计如下函数来进行小数处理
[u]复制代码[/u] 代码如下:
  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;     }   }
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部