Number.sign() in javascript

前端 未结 15 2325
日久生厌
日久生厌 2020-11-30 21:03

Wonder if there are any nontrivial ways of finding number\'s sign (signum function)?
May be shorter / faster / more elegant solutions than the obvious one



        
15条回答
  •  忘掉有多难
    2020-11-30 21:36

    Math.sign is not supported on IE 11. I am combining the best answer with Math.sign answer :

    Math.sign = Math.sign || function(number){
        var sign = number ? ( (number <0) ? -1 : 1) : 0;
        return sign;
    };
    

    Now, one can use Math.sign directly.

提交回复
热议问题