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

源码网商城

javaScript中Math()函数注意事项

  • 时间:2021-07-26 21:01 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:javaScript中Math()函数注意事项
1.不能显式地创建一个Math对象,直接使用它就可以了; 2.Math对象不能存储数据,和String,Date对象不同; 3.前面知道了parseInt()函数会通过消去小数点后面的一切,来使一个小数变成整数(因此24.999变为24).经常我们需要更精确的计算。 于是通过Math对象的这几个方法: round():当小数是0.5或者大于0.5的时候,向上入一位; ceil():始终向上舍入,因此23.75变成24,23.25也是如此; floor():始终向下舍入,因此23.75变成23,23.25也是如此;
<DOCTYPE html>
<html>
  <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
  <head>
    <title>Math函数</title>
  </head>
  <script type="text/javascript">
    var userInput=prompt("请输入一个数","");
    document.write("round()=",+Math.round(userInput));
    document.write("ceil()=",+Math.ceil(userInput));
    document.write("floor()=",+Math.floor(userInput));
    
  </script>
  <body>
  </body>
</html>

4.可以使用Math对象的random()方法,生成一个大于等于0,但小于1的随机小数。通常为了利用它,你需要再乘以某个数,然后在使用其中的一个舍入方法。
var diceThrow=Math.round(Math.random()*6)+1;
document.write("You threw a "+diceThrow);
附上Math对象的方法 1.丢弃小数部分,保留整数部分 parseInt(5/2) 2.向上取整,有小数就整数部分加1 Math.ceil(5/2) 3,四舍五入. Math.round(5/2) 4,向下取整 Math.floor(5/2) Math 对象的方法 FF: Firefox, N: Netscape, IE: Internet Explorer 方法 描述 FF N IE abs(x) 返回数的绝对值 1 2 3 acos(x) 返回数的反余弦值 1 2 3 asin(x) 返回数的反正弦值 1 2 3 atan(x) 以介于 -PI/2 与 PI/2 弧度之间的数值来返回 x 的反正切值 1 2 3 atan2(y,x) 返回从 x 轴到点 (x,y) 的角度(介于 -PI/2 与 PI/2 弧度之间) 1 2 3 ceil(x) 对一个数进行上舍入。 1 2 3 cos(x) 返回数的余弦 1 2 3 exp(x) 返回 e 的指数。 1 2 3 floor(x) 对一个数进行下舍入。 1 2 3 log(x) 返回数的自然对数(底为e) 1 2 3 max(x,y) 返回 x 和 y 中的最高值 1 2 3 min(x,y) 返回 x 和 y 中的最低值 1 2 3 pow(x,y) 返回 x 的 y 次幂 1 2 3 random() 返回 0 ~ 1 之间的随机数 1 2 3 round(x) 把一个数四舍五入为最接近的整数 1 2 3 sin(x) 返回数的正弦 1 2 3 sqrt(x) 返回数的平方根 1 2 3 tan(x) 返回一个角的正切 1 2 3 toSource() 代表对象的源代码 1 4 - valueOf() 返回一个 Math 对象的原始值 1. Math.abs(num) : 返回num的绝对值 2. Math.acos(num) : 返回num的反余弦值 3. Math.asin(num) : 返回num的反正弦值 4. Math.atan(num) : 返回num的反正切值 5. Math.atan2(y,x) : 返回y除以x的商的反正切值 6. Math.ceil(num) : 返回大于num的最小整数 7. Math.cos(num) : 返回num的余弦值 8. Math.exp(x) : 返回以自然数为底,x次幂的数 9. Math.floor(num) : 返回小于num的最大整数 10.Math.log(num) : 返回num的自然对数 11.Math.max(num1,num2) : 返回num1和num2中较大的一个 12.Math.min(num1,num2) : 返回num1和num2中较小的一个 13.Math.pow(x,y) : 返回x的y次方的值 14.Math.random() : 返回0到1之间的一个随机数 15.Math.round(num) : 返回num四舍五入后的值 16.Math.sin(num) : 返回num的正弦值 17.Math.sqrt(num) : 返回num的平方根 18.Math.tan(num) : 返回num的正切值 19.Math.E : 自然数(2.718281828459045) 20.Math.LN2 : 2的自然对数(0.6931471805599453) 21.Math.LN10 : 10的自然对数(2.302585092994046) 22.Math.LOG2E : log 2 为底的自然数(1.4426950408889634) 23.Math.LOG10E : log 10 为底的自然数(0.4342944819032518) 24.Math.PI : π(3.141592653589793) 25.Math.SQRT1_2 : 1/2的平方根(0.7071067811865476) 26.Math.SQRT2 : 2的平方根(1.4142135623730951) 以上所述就是本文的全部内容了,希望大家能够喜欢。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部