js的date对象和math对象的floor,pi,round和random随机数

<script type="text/javascript">
    var d = new Date();
    document.write(d);
    // data对象获取当前日期
    var d2 = new Date("2018-02-19");
    document.write(d2);
    document.write('PI')
    document.write(Math.PI);
    //-----------------------------------------------------------------
    document.write('四舍五入')
    document.write(Math.round(3.88));//四舍五入到整数 4
    document.write(Math.round(3.22));//四舍五入到整数 3
    //-----------------------------------------------------------------
    //floor(x) x 以下的最接近的整数
    document.write('<p>')
    document.write('2.77以下的整数是')
    document.write(Math.floor(2.77));
    //------------------------------------------------

    document.write('random随机数')
    document.write('<p>')
    document.write(Math.random());//0-1之间的随机数
    document.write('<p>')
    document.write('floor 和random一起使用')
    var r = Math.floor(Math.random() * 10);//0-9之间的随机数
    document.write(r);
    document.write('<p>')
    var r2 = Math.floor(Math.random() * 100);//0-99之间的随机数
    document.write(r2);
    document.write('<p>')
    var r2 = Math.floor(Math.random() * 100)+1;//1-100之间的随机数
    document.write(r2);

    document.write('<p>')
    var r3 = getRandom(2, 6);
    document.write(r3);

    //获取 从min到max之间的随机数(不包含max)
    function getRandom(min, max) {
        return Math.floor(Math.random() * (max - min)) + min;
         
    }
发布了55 篇原创文章 · 获赞 4 · 访问量 1404

猜你喜欢

转载自blog.csdn.net/BowenXu11/article/details/105200016
今日推荐