StackTips

Convert String to Long in Java

nilan avtar

Written by:

Nilanchala,  4 min read,  updated on September 17, 2023

The java.lang.Long class wraps a value of the primitive type long in an object. This class provides several methods to convert a long to a String and a String to a long, as well as other utility methods.

import java.text.NumberFormat;
import java.text.ParseException;
import java.util.Locale;

public class StringToLong {

	public static void main(String[] args) {

		// converting to long
		String number1 = "543210";
		long result = Long.valueOf(number1);
		System.out.println("Result=" + result);

		// remove all commas and then converting to long
		String bumber2 = "5,43,210";
		long result2 = Long.valueOf(bumber2.replaceAll(",", "").toString());
		System.out.println("Result=" + result2);

		// converting using Locale
		String number3 = "5,43,210";
		NumberFormat format = NumberFormat.getInstance(Locale.US);
		Number number = 0;
		try {
			number = format.parse(number3);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		long result3 = number.longValue();
		System.out.println("Result=" + result3);
	}

}

Mixing NumberFormat and Long.parseLong() isn’t a good idea always. NumberFormat can be locale-aware (in your example it uses the default locale for your computer) or you can explicitly specify format patterns, whereas the parseXXX() methods of Number subclasses only read “plain” numbers (optional minus sign+digits).

If you formatted it with NumberFormat, you should use NumberFormat.parse() to parse it back. However you shouldn’t depend on the default locale, try to specify one instead. Here in my above example, I am using Locale.US local.

If you don’t care about the format, consider using Long.toString() to convert a long value into string and Long.parseLong() to convert it back. It’s easier to use and works the same everywhere.

Java Collection APIs

The Collections framework in Java defines numerous different data structures in which you can store, group, and retrieve objects.

>> CHECK OUT THE COURSE

Keep exploring

Let’s be friends!