How to Convert a String to an Numeric in Java? Last Updated : 12 Feb, 2024 Comments Improve Suggest changes Like Article Like Report In Java programming, there are situations in which we must convert a string of numeric characters into one of the following data types: double, float, long, or int. To execute arithmetic operations and other numerical calculations, this conversion is necessary. We will look at how to convert a string to numeric types in Java in this tutorial. String to Numeric Conversion in JavaIn Java, we may utilize the wrapper classes and their corresponding parse methods to transform a string to a numeric type. For instance, Long.parseLong() for long, Float.parseFloat() for float, Double.parseDouble() for double, and Integer.parseInt() for converting to an int. Managing exceptions is essential because, if the string does not represent a valid numeric value, the conversion can generate a NumberFormatException. Syntax:Long varLong=Long.parseLong(str);int varInt = Integer.parseInt(intString);float varFloat = Float.parseFloat(floatString);double varDouble = Double.parseDouble(doubleString);Convert a String to an Numeric in JavaLet's look at several instances of how to convert various numeric types from a string: Java public class StringToNumericExample { public static void main(String[] args) { // Example 1: Convert String to int String intString = "123"; int convertedInt = Integer.parseInt(intString); System.out.println("Converted int: " + convertedInt); // Example 2: Convert String to long String longString = "9876543210"; long convertedLong = Long.parseLong(longString); System.out.println("Converted long: " + convertedLong); // Example 3: Convert String to float String floatString = "45.67"; float convertedFloat = Float.parseFloat(floatString); System.out.println("Converted float: " + convertedFloat); // Example 4: Convert String to double String doubleString = "123.456"; double convertedDouble = Double.parseDouble(doubleString); System.out.println("Converted double: " + convertedDouble); } } OutputConverted int: 123 Converted long: 9876543210 Converted float: 45.67 Converted double: 123.456 Explanation of the above Program:The examples show how to convert strings that represent numerical values to double, float, long, and int.For conversion, the parse methods of the corresponding wrapper classes—Integer, Long, Float, and Double—are used.After that, the converted numbers are shown on the console. Comment More infoAdvertise with us Next Article How to Convert a String to an Numeric in Java? R rahul9yw89 Follow Improve Article Tags : Java Java Programs Java-Strings strings Java Examples +1 More Practice Tags : JavaJava-StringsStrings Similar Reads How to Convert a String to an Long in Java ? In Java, String to Integer Conversion is not always effective as the size of the String can overpass Integer. In this article, we will learn the method provided by the Long class or by using the constructor of the Long class. Example of String to Long ConversionInput: " 9876543210"Output: 9876543210 2 min read How to Convert Char to String in Java? In this article, we will learn how to Convert Char to String in Java. If we have a char value like 'G' and we want to convert it into an equivalent String like "G" then we can do this by using any of the following three listed methods in Java. Using toString() method of Character classUsing valueOf( 5 min read How to Convert a String to a Character in Java? String and char are fundamental and most commonly used datatypes in Java. A String is not a primitive data type like char. To convert a String to char in Java, we have to perform character-based operations or have to process individual characters.In this article, we will learn how to convert a Strin 3 min read How to Convert a String Class to an Integer Class in Java? String in Java is used to store the sequence of characters in Java. A string can contain a character, a word, a sentence, or a paragraph. Integer is a Wrapper class that is used to store Integer values in Java. The range of the Integer class is from -2,147,483,648 to 2,147,483,647 (-2^31 to 2^31 - 1 3 min read Java Program to Convert String to Integer Array In Java, we cannot directly perform numeric operations on a String representing numbers. To handle numeric values, we first need to convert the string into an integer array. In this article, we will discuss different methods for converting a numeric string to an integer array in Java.Example:Below i 3 min read Like