Java Math abs() 方法

实例

返回不同数字的绝对 (正) 值:

  1. public class Main {
  2. public static void main(String[] args) {
  3. System.out.println(Math.abs(-4.7));
  4. System.out.println(Math.abs(4.7));
  5. System.out.println(Math.abs(3));
  6. }
  7. }

定义与用法

abs() 方法返回数字的绝对 (正) 值。


语法

下列情况之一:

  1. public static double abs(double number)
  1. public static float abs(float number)
  1. public static int abs(int number)
  1. public static long abs(long number)

参数值

参数描述
number必填。指定一个数字

技术细节

返回:表示指定数字绝对值的 doublefloatintlong

分类导航