CodeGym /Blog Jawa /Acak /Metode Java Math abs().
John Squirrels
tingkat
San Francisco

# Metode Java Math abs().

## Apa fungsi nilai absolut ing Matematika?

Ing matématika, nilai absolut saka nomer padha karo nilai positif saka nomer liwati.Fungsi nilai absolut nglirwakake tandha lan ngasilake nilai tanpa iku. Contone , mutlak +5 yaiku 5. Dene, mutlak saka -5 uga 5.

## Apa Math.abs() method() ing Jawa?

Kelas java.lang.Math menehi cara statis Math.abs(parameter) kanggo nemokake " Nilai Absolute " saka parameter.
Dadi, yen sampeyan ngliwati nomer positif, ayo ngomong Math.abs(5) bakal ngasilake 5. Kanggo negatif 5, Math.abs(-5) asile bakal padha, yaiku; 5.

``````
public static dataType abs(dataType parameter)
``````

## Tipe Data sing diidini

Cara abs () Jawa kakehan kanggo macem-macem jinis data. Jinis sing diidini kaya ing ngisor iki.
int float pindho dawa

``````
public class DriverClass {
public static void main(String args[]) {

int number = +5;
// Print the original number
System.out.println("Original Number = " + number);

// Printing the absolute value
// Calling the Math.abs() method
System.out.println("Absolute Number = " + "Math.abs( " + number + " ) = " + Math.abs(number));

number = -5;
// Print the original number
System.out.println("Original Number = " + number);

// Printing the absolute value
// Calling the Math.abs() method
System.out.println("Absolute Number = " + "Math.abs( " + number + " ) = " + Math.abs(number));

}
}
``````

### Output

Angka Asli = 5 Angka Mutlak = Math.abs( 5 ) = 5 Angka Asli = -5 Angka Mutlak = Math.abs( -5 ) = 5

### Panjelasan

Ing snippet kode ing ndhuwur, kita wis njupuk rong nomer. Nomer pisanan minangka integer positif yaiku +5. Nomer kapindho iku integer negatif ie -5. We pass loro nomer menyang Math.abs (nomer) cara. Cara ngasilake 5 kanggo loro input sing ora nggatekake tandha-tandhane.

``````
public class DriverClass {
public static void main(String args[]) {

int number = -0;
System.out.println("Original Number = " + number);
System.out.println("Math.abs( " + number + " ) = " + Math.abs(number) + "\n");

long number1 = -4499990;
System.out.println("Original Number = " + number1);
System.out.println("Math.abs( " + number1 + " ) = " + Math.abs(number1) + "\n");

float number2 = -92.45f;
System.out.println("Original Number = " + number2);
System.out.println("Math.abs( " + number2 + " ) = " + Math.abs(number2) + "\n");

double number3 = -63.7777777777;
System.out.println("Original Number = " + number3);
System.out.println("Math.abs( " + number3 + " ) = " + Math.abs(number3) + "\n");
}
}
``````

### Output

Angka Asli = 0 Math.abs( 0 ) = 0 Nomer Asli = -4499990 Math.abs( -4499990 ) = 4499990 Nomer Asli = -92,45 Math.abs( -92,45 ) = 92,45 Nomer Asli = -63,777777777(Math.77777777 63.7777777777 ) = 63.7777777777

### Panjelasan

Ing kode ndhuwur, kita wis dijupuk pindho, dawa lan float Nilai saliyane integer minangka input kanggo Math.abs () cara. Kita wis liwati kabeh nilai pamilike kanggo Math.abs () cara siji lan ditampilake asil ing console.

## Kasus Watesan

Kene sawetara kasus ngédap sing kudu njupuk care saka nalika nggunakake Math.abs () cara.

### Kanggo jinis data int lan dawa

Yen argumen kasebut nol positif utawa nol negatif, asile nol positif.
Math.abs(+0) = 0 Math.abs(-0) = 0
Kanggo Integer.MIN_VALUE utawa Long.MIN_VALUE output Math.abs () isih integer paling cilik utawa dawa kang negatif.
Math.abs(Integer.MIN_VALUE) = -2147483648 Math.abs(Long.MIN_VALUE) = -9223372036854775808

### Kanggo ngambang lan jinis data pindho

Yen argumen ora ana watese, asile positif tanpa wates.
Math.abs(Double.NEGATIVE_INFINITY) = Tanpa wates
Yen argumen kasebut NaN, asile NaN.
Math.abs(Double.NaN) = NaN

## Kesimpulan

Ing pungkasan postingan iki, sampeyan kudu ngerti metode Java Math.abs () . Sampeyan bisa nggunakake ing macem-macem jinis data numerik. Sampeyan bisa nemokake macem-macem aplikasi saben dina saka metode iki. Kaya biasane, kita ngajak sampeyan sinau kanthi latihan. Nganti saiki, terus sinau lan terus berkembang!
Komentar
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION