Integer.parseInt("-1000"); returns -1000 as the output.
Integer.parseInt("+500"); throws an exception.
How will I be able to recognize positive numbers with the "+" symbol before them without having to trim the symbol?
Try DecimalFormat like with the pattern "+#;-#". It will handle explicit signed parsing. Breakdown of the pattern:
;) is the positive pattern, it has to start with an + char- charExample:
DecimalFormat df = new DecimalFormat("+#;-#");
System.out.println(df.parse("+500"));
System.out.println(df.parse("-500"));
Outputs:
500
-500
                        One option is to use Java 7... assuming that behaves as documented:
Parses the string argument as a signed decimal integer. The characters in the string must all be decimal digits, except that the first character may be an ASCII minus sign '-' ('\u002D') to indicate a negative value or an ASCII plus sign '+' ('\u002B') to indicate a positive value.
It looks like this was a new feature introduced into Java 7 though. The JDK 6 docs only indicate that - is handled.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With