Java.lang.StrictMath.abs() 方法

描述

java.lang.StrictMath.abs(long a)方法返回long值的绝对值。如果参数不为负,则返回参数。 如果参数是否定的,则返回参数的否定。


声明

以下是 java.lang.StrictMath.abs() 方法的声明。

public static long abs(long a)

参数

a − 这是要确定其绝对值的参数。


返回值

该方法返回long值的绝对值。


异常

NA


示例

下面的例子展示了 java.lang.StrictMath.abs() 方法的使用。

package com.tutorialspoint;

import java.lang.*;

public class StrictMathDemo {

   public static void main(String[] args) {

      long l1 = 55475693, l2 = -7538911;
   
      // returns the absolute value of positive long value
      long lAbsValue = StrictMath.abs(l1);
      System.out.println("absolute value of " + l1 + " = " + lAbsValue);    

      // returns the absolute value of negative long value
      lAbsValue = StrictMath.abs(l2);
      System.out.println("absolute value of " + l2 + " = " + lAbsValue);
   }
}

让我们编译并运行上面的程序,这将产生下面的结果 −

absolute value of 55475693 = 55475693
absolute value of -7538911 = 7538911