Math数学方法--常用
2017-02-24 15:03
309 查看
Number
常用
Math.ceil() - - - - - - - - - - - - 向上取整.
Math.floor() - - - - - - - - - - - - 向下取整.
Math.round() - - - - - - - - - - - - 四舍五入.
Math.max() - - - - - - - - - - - 取最大值.
Math.min() - - - - - - - - - - - 取最小值.
Math.random() - - - - - - - - - - - 取[0,1)内的随机数.
Math.sin(x); 参数 x 的正弦值。===>返回值在 -1.0 到 1.0 之间。
Math.cos(x); 参数 x 的余弦值 ===>返回值在 -1.0 到 1.0 之间
一般
Math.abs() - - - - - - - - - - - 取绝对值.
Math.sqrt(x) - - - - - - - - - - - 取平方根.
Math.pow(x,y) - - - - - - - - - - - 取x的y次幂.
例子:
Math.random()==>扩展
Math.sin(x); 参数 x 的正弦值。===>返回值在 -1.0 到 1.0 之间。
Math.cos(x); 参数 x 的余弦值 ===>返回值在 -1.0 到 1.0 之间。
x:一个以弧度表示的角。将角度乘以 0.017453293 (2Math.PI/360)即可转换为弧度。
toFixed() 保留小数点.
常用
Math.ceil() - - - - - - - - - - - - 向上取整.
Math.floor() - - - - - - - - - - - - 向下取整.
Math.round() - - - - - - - - - - - - 四舍五入.
Math.max() - - - - - - - - - - - 取最大值.
Math.min() - - - - - - - - - - - 取最小值.
Math.random() - - - - - - - - - - - 取[0,1)内的随机数.
Math.sin(x); 参数 x 的正弦值。===>返回值在 -1.0 到 1.0 之间。
Math.cos(x); 参数 x 的余弦值 ===>返回值在 -1.0 到 1.0 之间
一般
Math.abs() - - - - - - - - - - - 取绝对值.
Math.sqrt(x) - - - - - - - - - - - 取平方根.
Math.pow(x,y) - - - - - - - - - - - 取x的y次幂.
例子:
// var num = 1.000002; // alert(Math.ceil(num));//2 // alert(Math.floor(num));//1 // var num = -2.3; // alert(Math.ceil(num));//-2 // alert(Math.floor(num));//-3 // var num = 12.52; // alert(Math.round(num));//13 //绝对值 把负数变成正数 // var num = -2.2; // alert(Math.abs(num));//2.2 //可以取到0 但是取不到1 //[2,5); //[0,1) --->乘3 [0,3) --->加2 [2,5) //setInterval(function(){ // console.log(Math.floor(Math.random()*3 + 2)); //},1000); //非数字字符串 不可以, var max = Math.max(12,34,45,12,7,55,"100"); var min = Math.min(12,34,45,12,7,55,"100"); // alert(max +"==="+ min); //比较数组 var arr = [2,1,4,56,67,"78",3]; //var max = Math.max(arr);//NaN //apply 有参数,方式传数组 var max = Math.max.apply(对象,arr);//null,[],{},new Date(),RegExp. //alert(max); var min = Math.min.apply(this,arr); //alert(min); //平方根 var sq = 2; alert(Math.sqrt(sq)); alert(Math.pow(2,3));// 2^3
Math.random()==>扩展
<!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <meta name="Author" content=""> <meta name="Keywords" content=""> <meta name="Description" content=""> <title>Document</title> <style type="text/css"> * { margin: 0; padding: 0;} a {text-decoration: none;} ul,li { list-style: none;} body { font-family: "Microsoft yahei";} div {width: 160px; height: 40px; line-height: 40px; float: left; text-align: center; color: 000;} </style> </head> <body> <script type="text/javascript"> // [0,1); // [5,10);==>左右边乘以5,再加5===>[0,5)==>[5,10). // [5,10];==>左右边乘以6,再加5===>[0,5)==>[5,11)=>[5,10] // [a,b)===>左右边乘以b-a,再加a===>[0,b-a)==>[a,b). // [a,b]===>左右边乘以b-a+1,再加a===>[0,b-a+1)==>[a,b]. // setInterval(function(){ // console.log(Math.floor(Math.random()*5+5)); // },1000); //返回[a,b]的随机整数. function randomNum(start,end){ return Math.floor(Math.random()*(end-start+1)+start); }; //rgb [0,255] opacity [0,1)==>[0,1] function randomColor(){ var r = Math.floor(Math.random()*256); var g = Math.floor(Math.random()*256); var b = Math.floor(Math.random()*256); var a = Math.random().toFixed(1); return "rgba("+r+","+g+","+b+","+a+")"; }; //16进制 function randomColor16(){ var r = Math.floor(Math.random()*256).toString(16); var g = Math.floor(Math.random()*256).toString(16); var b = Math.floor(Math.random()*256).toString(16); // r: aa +>a12ed // 0,1,3,4,5,6,7,8,9,a,b,c,d,e,f if(r.length<2)r = "c"+r; if(g.length<2)g = "6"+g; if(b.length<2)b = "1"+b; return "#"+r+g+b; } // setInterval(function(){ // console.log(randomColor()); // },500); var timer = null; var mark = true; function colorPlay(){ timer = setInterval(function(){ var box = document.createElement("div"); var color = randomColor16() box.style.background = color; box.innerHTML = color; document.body.appendChild(box); },500); }; colorPlay(); document.onclick = function(){ if(mark){ clearInterval(timer); }else{ colorPlay(); } mark = !mark; } </script> </body> </html>
Math.sin(x); 参数 x 的正弦值。===>返回值在 -1.0 到 1.0 之间。
Math.cos(x); 参数 x 的余弦值 ===>返回值在 -1.0 到 1.0 之间。
x:一个以弧度表示的角。将角度乘以 0.017453293 (2Math.PI/360)即可转换为弧度。
Math.PI --- π 圆周率 角度 弧度 90° --- π/2 180°--- π 1° --- π/180° 180°/π--- 1 x -- 0.09 180*0.09/π
相关文章推荐
- IOS开发 - math.h常用数学方法
- String\StringBuffer\StringBuilder的不同 基本常用类 Date时间类 Math数学类的基本方法
- 图形学中常用的几种数学方法
- [转]Math:为三角函数、对数函数和其他通用数学函数提供常数和静态方法。
- iOS math.h 常用数学函数
- linux Shell编程中常用的数学计算方法
- C# Math常用的方法
- 使用math.h中常用数学常数
- Math常用方法
- java math类有哪些常用方法
- skyline TEP学习心得(2):一些常用的三维计算数学方法
- 数学建模中常用思想和方法
- 城市规划研究分析常用数学方法之---层次分析法(AHP)
- Math 中常用的数学函数
- Math 中常用的数学函数
- Javascript中的Math对象常用方法
- Math 中常用的数学函数
- 常用数学证明方法
- 数学建模中常用思想和方法
- Java的Math常用方法