zoukankan      html  css  js  c++  java
  • 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也是如此;

     1 <DOCTYPE html>
     2 <html>
     3     <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
     4     <head>
     5         <title>Math函数</title>
     6     </head>
     7     <script type="text/javascript">
     8        var userInput=prompt("请输入一个数","");
     9        document.write("round()=",+Math.round(userInput));
    10        document.write("ceil()=",+Math.ceil(userInput));
    11        document.write("floor()=",+Math.floor(userInput));
    12        
    13     </script>
    14     <body>
    15     </body>
    16 </html>

    4.可以使用Math对象的random()方法,生成一个大于等于0,但小于1的随机小数。通常为了利用它,你需要再乘以某个数,然后在使用其中的一个舍入方法。

    var diceThrow=Math.round(Math.random()*6)+1;
    document.write("You threw a "+diceThrow);

  • 相关阅读:
    计算机组成原理:“性能”是什么?
    试题 基础练习 Huffuman树
    最小堆的插入删除函数
    特殊函数
    进程控制
    进程的描述
    进程管理
    生成和配置Linux操作系统
    系统调用
    Shell脚本编程
  • 原文地址:https://www.cnblogs.com/caofangsheng/p/4584346.html
Copyright © 2011-2022 走看看