Explain Math.floor(Math.random())

前端 未结 6 1190
轻奢々
轻奢々 2020-12-19 07:52

I have seen many places using Math.floor() and Math.random()

like below

$(\'a.random-color\').hover(function() { //mouseove         


        
6条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-19 07:55

    ~~Number is only the Math.floor() for positive numbers. For negative numbers, it is the Math.ceil().

    For positive numbers you can use:

    Math.floor(x) == ~~(x)
    
    Math.round(x) == ~~(x + 0.5)
    
    Math.ceil(x) == ~~(x + 1)
    

    For negative numbers you can use:

    Math.ceil(x) == ~~(x)
    
    Math.round(x) == ~~(x - 0.5)
    
    Math.floor(x) == ~~(x - 1)
    

提交回复
热议问题