Ahmed
Ahmed

Reputation: 15069

calculating magnitude of a number in java

I forgot the function in Java to calculate the magnitude of an integer. help please..

|5| = +5 = 5

|-5| = +5 = 5

so if a - b is - x

mag (a - b) is x a posstive number..

Upvotes: 9

Views: 15931

Answers (4)

Dan W
Dan W

Reputation: 5782

I believe you are looking for Math.abs(). Using this function:

5 = Math.abs(5) = Math.abs(-5)

Upvotes: 17

Oleksi
Oleksi

Reputation: 13097

Try Math.abs.

abs(-4) = 4;
abs(5) = 5;

Upvotes: 3

Lajos Arpad
Lajos Arpad

Reputation: 76874

Math.abs() is the function you are looking for.

Upvotes: 2

bouteillebleu
bouteillebleu

Reputation: 2493

The function's in the Math library and is called abs() (short for "absolute value").

Upvotes: 2

Related Questions