(str + " is not a valid numeric value.") Long num = decimalFormat.parse(str).longValue() Create an object of DecimalFormat class.ĭecimalFormat decimalFormat = new DecimalFormat("#") Program code 4: // Java Program to demonstrate the conversion of String into long using DecimalFormat class. Let’s create a Java program to convert a string to long numeric value using DecimalFormat class. We can also use in other way to parse a string into its numerical representation. This class is present in java.text package. Java provides a class called DecimalFormat that allows to convert a number to its string representation. Program code 3: // Java program to convert a string into a long using new Long(String).longValue().Ĭonverting String to Long using DecimalFormat Let’s create a Java program to convert String into a long object using longValue() method. Unboxing is a process by which we convert an object into its corresponding primitive data type. The longValue() method converts the long object into primitive long type value. Program code 2: // Java program to convert a string into a primitive long type using valueOf() method.Ĭonverting String to Long using new Long(String).longValue()Īnother alternative approach is to create an instance of Long class and then call longValue() method of Long class. Let’s write a Java program to convert a string into a long value using valueOf() method of Java Long wrapper class. We can invoke it using its class name.The general signature of valueOf() method is as below: public static Long valueOf(String str) It is a static utility method, so we do not need to create an object of class. The valueOf() method of Long class converts a string containing a long number into Long object and returns that object. We can also convert a string to long numeric value using valueOf() of Java long wrapper class. Call parseLong() method to convert a string to long value.Ĭonverting String to Long in Java using Long.valueOf() Program code 1: // Java program to convert a string into a primitive long type using parseLong() method. Let’s create a Java program to convert string to long in java using parseLong() of Java Long class. The parseLong() method throws an exception named NumberFormatException if the string does not contain a parsable long value. This method accepts a string containing the long representation to be parsed. The general signature of parseLong() method is as below: public static long parseLong(String s) We can call it simply using its class name. So, we do not need to create an object of class to call it. It reads long numeric values from the command-line arguments. The parseLong() of Long class is the static method. To convert string to a long, we use Long.parseLong() method provided by the Long class. Converting String to Long in Java using Long.parseLong() Let’s understand all four ways one by one with example programs. There are mainly four different ways to convert a string to wrapper Long class or primitive type long.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |