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);

  • 相关阅读:
    NHibernate介绍
    dwr配置文件dwr.xml详解
    架构设计师与SOA
    SOA是什么
    JDK常用命令
    在WPF的WebBrowser控件中抑制脚本错误
    通过编程计算一个游戏的胜率
    在C#中模拟大数乘法
    解决HttpWebRequest首次连接特别慢的问题
    布隆过滤器(Bloom Filter)
  • 原文地址:https://www.cnblogs.com/caofangsheng/p/4584346.html
Copyright © 2011-2022 走看看