# Convert a positive number to negative in C#

Advertisement

## Convert a positive number to negative in C#

Read more... Read less...

The same way you make anything else negative: put a negative sign in front of it.

```
var positive = 6;
var negative = -positive;
```

2009/08/28

Note to everyone who responded with

```
- Math.Abs(myInteger)
```

or

```
0 - Math.Abs(myInteger)
```

or

```
Math.Abs(myInteger) * -1
```

as a way to keep negative numbers negative and turn positive ones negative.

This approach has a single flaw. It doesn't work for all integers. The range of `Int32`

type is from "-2^{31}" to "2^{31} - 1." It means there's one more "negative" number. Consequently, `Math.Abs(int.MinValue)`

throws an `OverflowException`

.

The correct way is to use conditional statements:

```
int neg = n < 0 ? n : -n;
```

This approach works for "all" integers.

2009/08/28

```
int negInt = 0 - myInt;
```

Or guaranteed to be negative.

```
int negInt = -System.Math.Abs(someInt);
```

2009/08/28

Licensed under: CC-BY-SA with attribution

Not affiliated with: Stack Overflow

Email: [email protected]