Parse a String to a float or int in Java
This post will discuss how to parse a string to a double or float or int in Java.
1. Using Double.parseDouble() method
The standard solution to parse a string to get the corresponding double value is using the Double.parseDouble() method.
|
1 2 3 4 5 6 7 8 9 |
public class Main { public static void main(String[] args) { String s = "1.1"; double d = Double.parseDouble(s); System.out.println(d); } } |
The Double.parseDouble() method throws NumberFormatException if the string does not contain a parsable double. To avoid abrupt program termination, enclose your code within a try-catch block.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
public class Main { public static void main(String[] args) { String s = "1.1"; try { double d = Double.parseDouble(s); System.out.println(d); // 1.1 } catch (NumberFormatException e) { // string cannot be parsed to double } } } |
Note, you can’t parse strings like 1.1 using the Integer.parseInt() method. To get the integer value, you can cast the resultant double value.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
public class Main { public static void main(String[] args) { String s = "1.1"; try { int i = (int) Double.parseDouble(s); System.out.println(i); // 1 } catch (NumberFormatException e) { // string cannot be parsed to double } } } |
2. Using Float.parseFloat() method
Alternatively, you can use the Float.parseFloat() method to parse a string to a float.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
public class Main { public static void main(String[] args) { String s = "1.1"; try { float f = Float.parseFloat(s); System.out.println(f); // 1.1 } catch (NumberFormatException e) { // string cannot be parsed to float } } } |
To get the integer value, cast the resultant float value as discussed before.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
public class Main { public static void main(String[] args) { String s = "1.1"; try { int i = (int) Float.parseFloat(s); System.out.println(i); // 1 } catch (NumberFormatException e) { // string cannot be parsed to float } } } |
3. Using Integer.parseInt() method
To get the integer value represented by the string in decimal, you can use the Integer.parseInt() method, which parses the string as a signed decimal integer.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
public class Main { public static void main(String[] args) { String s = "1"; try { int i = Integer.parseInt(s); System.out.println(i); // 1 } catch (NumberFormatException e) { // string cannot be parsed to an integer } } } |
4. Using Number class
To parse text from the beginning of the given string to produce a number, you may want to use the parse() method of the NumberFormat class. This method might not use the entire string and throws ParseException if the string starts with any unparseable non-numeric character.
The NumberFormat.parse() method returns a Number class instance, which offers intValue(), longValue(), floatValue(), doubleValue(), byteValue(), and shortValue() methods to get the value of the specified number as a corresponding method type.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import java.text.NumberFormat; import java.text.ParseException; public class Main { public static void main(String[] args) { String s = "1.1"; try { Number number = NumberFormat.getNumberInstance().parse(s); double d = number.doubleValue(); System.out.println(d); // 1.1 int i = number.intValue(); System.out.println(i); // 1 } catch (ParseException ex) { // Unparseable number } } } |
That’s all about parsing a string to a float or int in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)