views:

294

answers:

3

Hi,

How can I make an integer negative in C#?

abc = 5645307;
// how to make abc -5645307 ?
+13  A: 

Maybe I'm missing something:

abc = -abc;

If you want it to be negative whether it was negative beforehand or not, you would use:

abc = -Math.Abs(abc);
Jon Skeet
That's how I read the question, too.
T.J. Crowder
Skeet's gravitational pull is so massive he just attracts points. Eventually he will be crushed by their weight into a singularity, at which point we'll be sucked through the event horizon and Jeff will poke his eyes out and scare the bejesus out of everybody.
Will
@Will: Do you mean Jeff will poke *my* eyes out, or his own?
Jon Skeet
you mean it's forming a black hole that has already become a neutron star ? beware Jeff..
this. __curious_geek
@Jon no I mean Jeff will play Sam Neil's role (my comment isn't very funny but its less so if you don't get the reference: http://www.imdb.com/title/tt0119081/)
Will
+4  A: 

This is how you can convert your int value to minus in c#

abc = abc > 0 ? abc*(-1) : abc;
this. __curious_geek
+1  A: 

how about....

xyz = xyz * (-1)

Bhaskar
what if xyz is already negative ?
this. __curious_geek
I agree....in that case we would need to do an Abs on that number, i.e. xyz = Math.Abs(xyz) * (-1). Thanks for pointing out....
Bhaskar