Always return positive value

前端 未结 7 1461
清酒与你
清酒与你 2020-12-11 14:15

I have a number it could be negative or positive but I simply want to return the positive value.

-4 -> 4
5 -> 5

I know I can do a sim

7条回答
  •  死守一世寂寞
    2020-12-11 15:17

    Yet another way with Math.CopySign:

    var negativeNum = -5;
    var positiveNum = Math.CopySign(negativeNum, 1); // 5
    

提交回复
热议问题