I am trying to convert a String to an Integer value.
example : "3,879" to 3879.
How to do that using java.text.Numberformat;
or If there is any other way to do that.
Thanks in advance.
I am trying to convert a String to an Integer value.
example : "3,879" to 3879.
How to do that using java.text.Numberformat;
or If there is any other way to do that.
Thanks in advance.
You could do something like this:
String myNumber = "3,359";
myNumber = myNumber.replaceAll(",", "");
int test = Integer.parseInt(myNumber);
System.out.println("" + test);
You can do it like this also:
This answer uses code from above link:
NumberFormat.getNumberInstance(java.util.Locale.US).parse("265,858");
int test = 0;
try {
test = NumberFormat.getNumberInstance(java.util.Locale.US).parse("265,858").intValue();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("" + test);
You can strip the commas with replaceAll from the string and use parseInt.
int a = Integer.parseInt( yourstr.replaceAll("[^0-9]",""));