How to convert String to Double in Java

With the introduction of different data types, the need for conversion of one data type to another has come into being. In this article, we are going to see how we can convert from a String data type into Double data type.

Conversion Modes

There are two ways in which String data can be converted into double. They are:

  • Using the static method parseDouble(String) of the java.lang.Double wrapper class
  • Using the static method valueOf(String) of the java.lang.Double wrapper class
  • Using the constructor of Double wrapper class that takes String as its parameter

Let us see both the modes and how to do them.

parseDouble(String) method

The class java.lang.Double has a static method called parseDouble(String) that allows the programmer to easily convert a String containing decimal data into the corresponding Double value. The function returns a Double value.
Let us see a code snippet to illustrate the same:

The above code on execution provides the following output:

35.126

What if String is not convertible to double

If String is not convertible to double, you will get below exception.

Exception in thread “main” java.lang.NumberFormatException: For input string: “35.126df”
at java.base/jdk.internal.math.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2054)
at java.base/jdk.internal.math.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
at java.base/java.lang.Double.parseDouble(Double.java:543)
at org.arpit.java2blog.java8.ConvertStringToDouble.main(ConvertStringToDouble.java:8)

Using valueOf(String) method

You can also use valueOf(String) method to convert String to double in java. You can call Double’s doubleValue() to convert it to primitive type.

When you run above program, you will get below output.

35.126

Using the Double class constructor

The other method to convert to double is by using the parameterized constructor of java.lang.Double class that accepts a String as a parameter input. This creates an object of the Double class that stores the value of the String data sent and can be accessed using its doubleValue() function or by accessing the wrapper class object itself.

Let us see the code snippet:

The above code on execution provides the following output:

35.126

That’s all about String to Double conversion.

Related Posts

  • 09 January

    Convert set to String in Python

    Table of ContentsWays to convert set to string in PythonUsing the str() functionUsing the repr() functionUsing the join() functionUsing the map() functionConclusion Sets are an unordered collection of elements in Python. It does not contain duplicate elements and can be defined using the set() function in Python. Ways to convert set to string in Python […]

  • Convert String to Array in java
    11 December

    Convert String to Array in Java

    Table of ContentsHow to convert String to Array in JavaUsing toArray() method of SetUsing the split() method of String classUsing StringTokenizor classUsing the split() method of StringUtils classUsing split() method of Pattern classConclusion When developing applications in Java there are many cases where we will find ourselves converting data from one type to the other. […]

  • Convert float to int in Python
    19 June

    Convert float to int in Python

    Table of ContentsUsing the int() function to convert float to int in PythonUsing the math module functions to convert float to int in PythonUsing ceil()Using trunc()Using floor Python provides several in-built functions and modules for datatype conversions. Floating-point numbers can be easily converted to integers using some of these in-built functions. This tutorial will discuss […]

  • Java BigDecimal to Double
    17 May

    Java BigDecimal to Double

    Learn about how to convert BigDecimal to Double in java

  • Java Set to Array
    12 May

    Java Set to Array

    Table of Contents1. Using Java 8’s Stream2. Using toArray()3. Using toArray(IntFunction) [Java 11]4. Using System.arraycopy()5. Using Arrays.copyOf6. Using simple iteration7. Using Guava library7.1 Using FluentIterable7.2 Using Iterables In this post, we will learn java set to array conversion. There are many ways to convert set to an array. 1. Using Java 8’s Stream If you […]

  • Convert Date to LocalDate in java
    12 January

    Java Date to LocalDate

    Table of ContentsUsing toInstant() method of Date classUsing toInstant() method of Date classUsing java.sql.Date In this post, we will see how to convert Date to LocalDate in java. Sometimes, we may need to convert Date to new Java 8 APIs and vice versa. There are multiple ways to convert Date to LocalDate in java. Read […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.