Java Program to convert double type variables to string

Java Program to convert double type variables to string

What are type conversion?

Type conversion, also known as type casting, in Java is the process of converting one data type to another data type. There are two types of type conversion in Java: Implicit type conversion and Explicit type conversion.

In this Article, we will write a program to convert double type variables to string.

“double” to string conversion :

The conversion of double type variables to string in Java refers to the process of converting a double type value to a String type value. This can be done using the Double.toString method or the String.valueOf method, as well as other methods like DecimalFormat or String.format.

1. Program to Convert double to String using String.valueOf() method :

String.valueOf is a static method in the java.lang.String class that returns a string representation of the argument passed to it. It can be used to convert different data types, including int, long, float, double, char, boolean, and Object to a String type value.

Example:

Run
// Importing all the library functions
import java.util.*;
import java.lang.Math;

public class Main{
  public static void main(String[] args){
    // Initializing the double variable
    double myDouble = 4.13;
    
    // converting double to string using tostring method
    String myString = Double.toString(myDouble);
    
    // Printing the converted variable
    System.out.println("Double value: " + myDouble);
    System.out.println("String value: " + myString);
  }
}
Output:

Double value: 4.13
String value: 4.13

Explanation :

In the above example, the value of myDouble is 4.13, and after the conversion using the double.toString function, the value of myString is “42.23”.

2. Program to Convert double to String using double.toString() method :

The Double.toString method in Java, converts a double type value to its string representation. The method returns a string that represents the value of the double argument.

Example:

Run
// Importing all the library functions
import java.util.*;
import java.lang.Math;

public class Main{
   public static void main(String[] args){
      // Initializing the double variable
      double num = 2.15;
      
      // Converting the double variable to String using toString Method
      String str = Double.toString(num);
      
      // Printing the double and string variable
      System.out.println("Double value: " + num);
      System.out.println("String value: " + str);
   }
}
Output:

Double value: 2.15
String value: 2.15

Explanation:

In the above example, the double value 2.15 is passed as an argument to the Double.toString() method, which returns its string representation. The string representation of the double value is then stored in the str variable and is printed to the console.

3. Program to Convert double to String using Decimal Format class :

The DecimalFormat class is a subclass of the NumberFormat class in Java, and it is used to format numbers with a specified number of digits after the decimal point. This class allows you to define a pattern for the format of a decimal number, such as the number of decimal places, the use of grouping separators.

Example :

Run
// Importing all the library functions
import java.util.*;
import java.lang.Math;
import java.text.DecimalFormat;

public class Main{
   public static void main(String[] args){
      // Initializing the double variable 
      double num = 2.156789;
      
      // Declaration of new object of decimal format
      DecimalFormat df = new DecimalFormat("#.##");
      
      // Converting the double variable to String
      String str = df.format(num);
      
      // Printing the variable of double and string 
      System.out.println("Double value: " + num);
      System.out.println("String value: " + str);
   }
}
Output:

Double value: 2.156789
String value: 2.16

Explanation:

In the above example, The double value 2.156789 is passed as an argument to the format() method of a DecimalFormat object, which is created with the pattern “#.##”.

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription