Integer in Java ParseInt, when ” is the first exception

Integer parseInt( “ – 1000”); Returns - 1000 as output

Integer. parseInt(“500”); Throw exception

How can we recognize positive numbers with their previous "" symbols without having to cut symbols?

Solution

Try the decimalformat pattern "#; –#" It handles explicit signature resolution Pattern subdivision:

>The first part (before) is a positive pattern, which must start with one character > the second part is negative, which must start with a – char

Example:

DecimalFormat df = new DecimalFormat("+#;-#");
System.out.println(df.parse("+500"));
System.out.println(df.parse("-500"));

Output:

500
-500
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>