Hello, I would like to know, How can I convert a positive number ( int type) to a negative number in C# and vice-versa ( negative to positive number using C#)?
To Convert positive number to negative number using C#
int myNegInt = System.Math.Abs(myNumber) * (-1);
or Simply
int NegNumber= -System.Math.Abs(myNumber)
To Convert negative number to positive number in C#
var NegativeNumber= -1010;
var PositiveNumber=Math.Abs(NegativeNumber); //output 1010
That's it.
There is a simple way to convert positive to negative in C#
var positive = 5;
var negative = -positive;
and to convert negative to positive in C#
var getPositiveValue = (Math.Abs(-5));
Thanks.
Subscribe to our weekly Newsletter & Keep getting latest article/questions in your inbox weekly