How to convert a negative number to positive?


Question

How can I convert a negative number to positive in Python? (And keep a positive one.)

1
84
12/31/2015 9:29:24 PM

Accepted Answer

>>> n = -42
>>> -n       # if you know n is negative
42
>>> abs(n)   # for any n
42

Don't forget to check the docs.

175
10/5/2010 11:28:32 PM

simply multiplying by -1 works in both ways ...

>>> -10 * -1
10
>>> 10 * -1
-10

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon