您的位置:首页 > 其它

Math数学方法--常用

2017-02-24 15:03 302 查看
Number

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/π


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: