Data type

  • Thread starter Thread starter MAY
  • Start date Start date
...
how to convert a negative int (e.g -10)
to positive (ie.10).

There's several ways. Some examples:

int x = -10;
x = -x; // invert from negative to positive

....or you can use the Math.Abs method, if you want a positive value
regardless of whether it was negative or positive before.

int y = -10;
y = Math.Abs(y);

// Bjorn A
 
...


There's several ways. Some examples:

int x = -10;
x = -x; // invert from negative to positive

...or you can use the Math.Abs method, if you want a positive value
regardless of whether it was negative or positive before.

int y = -10;
y = Math.Abs(y);

// Bjorn A

Or if you don't want to use the Math class,
if( x < 0 )
x = -x ;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top