Finding absolute value of a number without using Math.abs()

后端 未结 10 1540
轮回少年
轮回少年 2021-02-02 11:16

Is there any way to find the absolute value of a number without using the Math.abs() method in java.

10条回答
  •  闹比i
    闹比i (楼主)
    2021-02-02 12:02

    Here is a one-line solution that will return the absolute value of a number:

    abs_number = (num < 0) ? -num : num;
    

提交回复
热议问题