Java中的Integer.parseint,首先是'+'时的异常 [英] Integer.parseint in Java, exception when '+' comes first
本文介绍了Java中的Integer.parseint,首先是'+'时的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Integer.parseInt( - 1000);
返回-1000作为输出。
Integer.parseInt("-1000");
returns -1000 as the output.
Integer.parseInt(+ 500);
抛出异常。
我如何才能识别正数在他们之前的+符号
而不必修剪符号?
How will I be able to recognize positive numbers with the "+" symbol before them without having to trim the symbol?
推荐答案
尝试 DecimalFormat
使用模式+#; - #
。它将处理显式签名解析。模式细分:
Try DecimalFormat
like with the pattern "+#;-#"
. It will handle explicit signed parsing. Breakdown of the pattern:
- 第一部分(在
之前;
)是正面的模式,它必须以+
char开头 - 第二部分是否定的,必须以<$ c $开头c> - char
- The first part (before
;
) is the positive pattern, it has to start with an+
char - The second part is the negative and has to start with a
-
char
示例:
DecimalFormat df = new DecimalFormat("+#;-#");
System.out.println(df.parse("+500"));
System.out.println(df.parse("-500"));
输出:
500
-500
这篇关于Java中的Integer.parseint,首先是'+'时的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文