negative int to positive int value

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Does anyone know if there is a easy way to change a negitive int value into a
positive int value.

int a = -105 > change this to 105.

With other words is there a command to change to neg sign into pos sign ?

Bardo Versteeg
 
If you mean to change it after a value has already been assigned to 'a':

Negative or Positive --> Positive
a = Math.Abs(a)

Reverse the sign
a = -(a)
or
a = a * -1
 
bardo said:
Does anyone know if there is a easy way to change a negitive int value into a
positive int value.

int a = -105 > change this to 105.

With other words is there a command to change to neg sign into pos sign ?

a = a<0 ? -a : a; // if a is negative >>or<< positive

Till
 
Thanks Bob , Stephany and Till,

Math.abs(<value>) does the trick.
Also thanks for the other tips. a = -(a) is a handy one to remember.

Bardo
 
Back
Top