Convert String to Double in Java

Read Time:4 Minute, 56 Second

Table of Contents

It is commonly used when handling mathematical operations on a double number string. The entered data is received as a string whenever we retrieve data from a textfield or textarea. We must convert the string to double if the input data is double.

The aim is to transform a String into a Double in Java.

This tutorial shall go over how to convert a String to a Double in Java. You can convert a String to double in three different methods.

  • Using Double.parseDouble(String) method
  • Convert String to Double in Java using Double.valueOf(String)
  • Java Convert String to double using the constructor of Double class – The constructor Double(String) is deprecated since Java version 9
  • DecimalFormat parse()

Convert String to Double in Java examples

To complete the objective, we can use a variety of approaches:

SUGGESTED READ

Using Double.parseDouble()

The method Double.parseDouble() is used to do this. In addition, the static function of the Double class is parseDouble(). This function returns the double equivalent of the supplied string argument. If the string str provided is null, this method will throw NullPointerException, and if the string format is invalid, it will throw NumberFormatException. For example, if the given string is “6042.80ab,” this method will throw a NumberFormatException.

The following is the signature of the parseDouble() method:

The syntax is as follows:

or

This procedure throws an Exception. When the String does not include a parsable double, a NumberFormatException is thrown.

SUGGESTED READ

Let’s look at some simple java code to convert a string to a double.

Example 1: using Double.parseDouble()

Example 2: using Double.parseDouble()

Example 3: Program for converting a String to double using parseDouble(String)

Consider the following scenario: we want to compute the pricing after receiving a discount on the item’s initial price. The input parameters are such that the item’s original price and discount come from your billing system as text. We want to conduct an arithmetic operation on these numbers to determine the new price after subtracting the discount from the original price.

In the following example code, we’ll learn how to utilize the Double.parseDouble() method to convert a String value to a double:

Using Double.valueOf()

The valueOf() method of the Double wrapper class in Java works the same way as the parseDouble() method in the previous Java example.

The syntax is as follows:

SUGGESTED READ

After conversion, the value of dNumber would be 5021.1850.

Let’s look at a complete conversion example utilizing the Double.valueOf(String) function.

Example 1: Program for converting a String to double using valueOf(String)

Example 2: Program for converting a String to double using valueOf(String)

Example 3: Program for converting a String to double using valueOf(String)

Using constructor of Double class

Since Java version 9, the constructor Double(String) has been deprecated. The constructor of the Double class parses the String argument that we send in and produces an equivalent double value.

The syntax is as follows.

or

SUGGESTED READ

We can use this constructor to build a new Double-object by giving the String we want to convert.

Example 1: Java Program for converting a String to double using the constructor of Double class

Example 2: Java Program for converting a String to double using the constructor of Double class

DecimalFormat parse()

We can use a DecimalFormat when a String expressing a double has a more complicated format. You can use it to convert a formatted string to a double. For instance, if String is “8,11,666.43d,” we may use DecimalFormat to convert this String to:

Because the parse() method produces a Number instance, we must use doubleValue() to obtain the double primitive type. If the String is not formatted correctly, this function will throw a ParseException. We can convert a decimal-based monetary value without eliminating non-numeric symbols in the following way:

The DecimalFormat.parse method, like Double.valueOf, produces a number, which we may convert to a basic double using the doubleValue method. The setParseBigDecimal method is also used to force DecimalFormat.parse to return a BigDecimal.

The DecimalFormat is usually more complicated than we need. Therefore, we should use the Double instead. Instead, use parseDouble or Double.valueOf. This method parses the text that is passed to it. It returns the number using a string from the beginning position.

SUGGESTED READ

This procedure throws an Exception. If the String’s beginning is not in a parsable format, a ParseException is thrown. Using the parse() method, this sample code parses a structured text string containing a double value:

Conversions that are not valid

For incorrect numeric Strings, Java provides a standardized interface. Double.parseDouble, Double.valueOf, and DecimalFormat are a few examples. When we supply null to parse, we get a NullPointerException. Similarly, Double.parseDouble and Double.parseDouble.parseDouble.parseDouble.

When we supply an invalid String that can’t be processed to a double, valueOf throws a NumberFormatException (such as &). Finally, when we provide an invalid String to DecimalFormat.parse, it raises a ParseException.

Conclusion

There are several methods for converting a Java String to a double. Today, we’ve looked at some of the most common methods for converting a Java string to a double primitive data type or a Double-object. Because Java allows autoboxing, you can interchangeably use the double primitive type and the double-object.

Overall, Java gives us various ways to convert Strings to double numbers. In general, we advise you to use Double. Unless a boxed Double is required, use parseDouble.

SUGGESTED READ

Source: https://www.codeunderscored.com/convert-string-to-double-in-java/