Metoda Java Math.abs().

The java.lang.Math.abs() metoda returnează valoarea absolută (pozitivă) a unei valori int. Această metodă oferă valoarea absolută a argumentului. Argumentul poate fi int, double, long și float.

Sintaxă:

 public static int abs(int i) public static double abs(double d) public static float abs(float f) public static long abs(long lng)  

Parametri:

 The argument whose absolute value is to be determined  

Întoarcere:

 This method returns the absolute value of the argument  
  • Dacă oferim o valoare pozitivă sau negativă ca argument, această metodă va avea ca rezultat o valoare pozitivă.
  • Dacă argumentul este Infinit , va rezulta aceasta metoda Infinitul pozitiv .
  • Dacă argumentul este NaN , această metodă va reveni NaN .
  • Dacă argumentul este egal cu valoarea Integer.MIN_VALUE sau Long.MIN_VALUE, cea mai negativă valoare int reprezentabilă sau lungă, rezultatul este aceeași valoare, care este negativă.

Exemplul 1:

 public class AbsExample1 { public static void main(String args[]) { int x = 78; int y = -48; //print the absolute value of int type System.out.println(Math.abs(x)); System.out.println(Math.abs(y)); System.out.println(Math.abs(Integer.MIN_VALUE)); } }  
Testează-l acum

Ieșire:

 78 48 -2147483648  

Exemplul 2:

 public class AbsExample2 { public static void main(String args[]) { double x = -47.63; double y = -894.37; //print the absolute value of double type System.out.println(Math.abs(x)); System.out.println(Math.abs(y)); System.out.println(Math.abs(7.0 / 0)); } }  
Testează-l acum

Ieșire:



 47.63 894.37 Infinity  

Exemplul 3:

 public class AbsExample3 { public static void main(String args[]) { float x = -73.02f; float y = -428.0f; //print the absolute value of float type System.out.println(Math.abs(x)); System.out.println(Math.abs(y)); } }  
Testează-l acum

Ieșire:

 73.02 428.0  

Exemplul 4:

 public class AbsExample4 { public static void main(String args[]) { long x = 78730343; long y = -4839233; //print the absolute value of long type System.out.println(Math.abs(x)); System.out.println(Math.abs(y)); System.out.println(Math.abs(Long.MIN_VALUE)); } }  
Testează-l acum

Ieșire:

 78730343 4839233 -9223372036854775808  

Top Articole

Categorie

Articole Interesante