Integer.parseInt("-1000");
返回 -1000 作为输出。
Integer.parseInt("+500");
抛出异常。
我如何识别前面带有“+”符号的正数 无需修剪符号?
最佳答案
尝试 DecimalFormat
就像模式 "+#;-#"
一样。它将处理显式签名解析。模式分解:
- 第一部分(在
;
之前)是肯定的模式,它必须以+
字符开头 - 第二部分是负数,必须以
-
字符开头
例子:
DecimalFormat df = new DecimalFormat("+#;-#");
System.out.println(df.parse("+500"));
System.out.println(df.parse("-500"));
输出:
500
-500
关于java - Java中的Integer.parseint,当 '+'先出现时异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10685906/