My question: how to make -2147483648 a positive number in Java?
There is no value that is representable as an int
that is equal to +2147483648
.
So the idea of "making -2147483648 positive" as an int
has no meaning. You simply cannot do it1.
But you could represent +2147483648
as a long
or as a BigInteger
(or a few other ways with various caveats.)
What should I do?
Use long
(though that has the same issue with Long.MIN_VALUE
) or BigInteger
or BigDecimal
or some other representation.
Or just treat this as a "feature" rather than a problem ... and work around it. For example, remove the sign after you have put it into the StringBuilder
or treat -2147483648 as a special case.
1 - It is a mathematical fact that 2's complement signed representations are not symmetric about zero. There is one more negative number ( < 0) than positive ( > 0), and therefore one negative number doesn't have a corresponding positive number.