How to convert a positive number to negative number and negative to positive in C#?


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#)?

 

 


Asked by:- bhanu
0
: 12534 At:- 6/3/2019 6:00:56 PM
C# positive-to-negative-int negative-to-positive-int







2 Answers
profileImage Answered by:- pika

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.

2
At:- 6/6/2019 7:38:48 AM


profileImage Answered by:- bhanu

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.

0
At:- 1/19/2022 1:47:04 PM






Login/Register to answer
Or
Register directly by posting answer/details

Full Name *

Email *




By posting your answer you agree on privacy policy & terms of use