Convert String with commas to Long – Java

A short guide to show you how to convert a String with commas to a long type.

1. For a normal String, you can use Long.valueOf to convert it directly.


	String bigNumber = "1234567899";
	long result = Long.valueOf(bigNumber);

2. For a String with commas, you can use java.text.NumberFormat to convert it.


	String bigNumber = "1,234,567,899";
	NumberFormat format = NumberFormat.getInstance(Locale.US);
        Number number = 0;
	try {
		number = format.parse(bigNumber);
	} catch (ParseException e) {
		e.printStackTrace();
	}
	long result = number.longValue();

3. Alternatively, if you don’t care about Locale, just replace all the commas.


	String bigNumber = "1,234,567,899";
	long result3 = Long.valueOf(bigNumber.replaceAll(",", "").toString());

Done.

References

  1. NumberFormat JavaDoc
author image

mkyong

Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities. Read all published posts by

Comments

avatar
3000