How to Parse Number String Containing Commas into an Integer in Java

How to parse number string containing commas into an integer in java?

Is this comma a decimal separator or are these two numbers? In the first case you must provide Locale to NumberFormat class that uses comma as decimal separator:

NumberFormat.getNumberInstance(Locale.FRANCE).parse("265,858")

This results in 265.858. But using US locale you'll get 265858:

NumberFormat.getNumberInstance(java.util.Locale.US).parse("265,858")

That's because in France they treat comma as decimal separator while in US - as grouping (thousand) separator.

If these are two numbers - String.split() them and parse two separate strings independently.

How to convert string ( that contains comma separated values.) to an Integer

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:

link for number formatpackage

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);

How to convert a string of comma separated numbers to integers?

First split the values with comma using the split() method like this.

arr.split(',')

After that you will get each value separated in an array which will be accessible by giving array index. So arr[0] will include 108 and arr[1] will include 109. Now all that's left to do is parse them individually.

parseInt(arr[0]) 
parseInt(arr[1])

cast String with period and comma to int

final String a = "9.000,00";
final NumberFormat format = NumberFormat.getInstance(Locale.GERMAN); // Use German locale for number formats
final Number number = format.parse(a); // Parse the number
int i = number.intValue(); // Get the integer value

Reference

How to change a string of numbers, spaces, and commas, to an arraylist of integers

You can achieve this in one line by using java 8 streams using String.trim() will trim all the spaces and Integer.valueOf() change string to Integer

List<Integer> l= Arrays.stream("1, 2, 3, 4, 11, 23".split(",")).map(item->Integer.valueOf(item.trim())).collect(Collectors.toList());
System.out.println(l);

How to convert string numbers into comma separated integers in java?

I assume 123455 is a String.

String s = 123455;
String s1 = s.substring( 0 , 1 ); // s1 = 1
String s2 = s.substring( 1 , 3 ); // s2 = 23
String s3 = s.substring( 2 , 7 ); // s3 = 455
s1 = s1 + ',';
s2 = s2 + ',';
s = s1 + s2; // s is a String equivalent to 1,23,455

Now we use static int parseInt(String str) method to convert String into integer.This method returns the integer equivalent of the number contained in the String specified by str using radix 10.

Here you cannot convert s ---> int . Since int does not have commas.If you try to convert you will get the following exception java.lang.NumberFormatException

you should use DecimalFormat Class. http://download.oracle.com/javase/1.4.2/docs/api/java/text/DecimalFormat.html



Related Topics



Leave a reply



Submit