Convert double to string in java – Java Program to Convert double to String

Convert double to string in java: In the previous article we have discussed Java Program to Convert float to String

In this article we will see how to convert a character to string.

Program to Convert double to String

How to convert double to string in java: Before going into the program, let’s see some examples of both the types.

Example-1 double type

double a = 3.123456789
double b = 3.5E02
Example-2: String type 

String a = "BtechGeeks"; 
String b = "B";

Let’s see different ways to convert double type to String type.

Beginners and experienced programmers can rely on these Best Java Programs Examples and code various basic and complex logics in the Java programming language with ease.

Method 1 : Java Program to Convert double to String Using valueOf() method

How to convert a double to a string in java: Double can be converted to string by using  valueOf() lets see how it works.

String.valueOf() is a method  which will simply typecasts below-given parameter to strings always. It is an inbuilt method of String class in java.

Approach :

  1. Take a Double value and store it in a double  variable input1
  2. Then pass that input1 variable as parameter to String.valueOf( ) method which will convert the double to string value and return it .
  3. Store that string value in a String variable output.
  4. Display the result.

Program :

import java.util.Scanner;

public class Main

{
    public static void main(String[] args)
    {
        // creating scanner object
        Scanner sc = new Scanner(System.in);
        // input a double through scanner class
        System.out.println("Enter a Double value : ");
        double input1=sc.nextDouble();
        // converting to string
        String output = String.valueOf(input1);
        System.out.println("Converted String is : "+output);
    }
}
Output : 

Enter a Double value : 131.123456
Converted String is : 131.123456

Method 2 : Java Program to Convert double to String Using toString() method

Double can be converted to string by using  toString() , let’s see how it actually works.

Whenever we use print statement in java,  toString() method of Object class in java is always called. toString() method of Object Class in java is always called by directly or indirectly. Here we are using this directly to convert the Double to string itself .

Approach :

  1. Take a Double value and store it in a double  variable input1
  2. Then pass that input1 variable as parameter to Double.toString ( ) method which will convert the double to string value and return it .
  3. Store that string value  in a variable output .
  4. Display the result .

Program :

import java.util.Scanner;

public class Main
{
public static void main(String[] args)
    {
        // creating scanner object
        Scanner sc = new Scanner(System.in);
        // input a double through scanner class
        System.out.println("Enter a Double value : ");
        double input1=sc.nextDouble();
        // converting to string
        String output = Double.toString(input1);
        System.out.println("Converted String is :"+output);
    }
}
Output : 

Enter a Double value : 131.123456 
Converted String is : 131.123456

Method 3 : Java Program to Convert double to String Using  “+” operator

Double can be converted to string by using  “+” operator. Let’s see how it actually works.

Approach :

  1. Take a Double value  and store it in a double variable input1
  2. Take a string variable and concatenate “+” with the input variable which will treated as string.
  3. Display the result.

Program :

import java.util.Scanner;

public class Main

{
    public static void main(String[] args)
    {
        // creating scanner object
        Scanner sc = new Scanner(System.in);
        // input a double value through scanner class
        System.out.println("Enter a Double value : ");
        double input1=sc.nextDouble();
        // converting to string
        String output = " " + input1;
        System.out.println("Converted String is :"+output);
    }
}
Output : 

Enter a Double value : 131.123456 
Converted String is : 131.123456

Method 4 : Java Program to Convert double to String Using format() method

Double can be converted to string by using format() method, let’s see how it actually works.

Approach :

  1. Take a Double value and store it in a double  variable input1
  2. Then pass that input1 variable as parameter to String.format ( ) method which will convert the double to string value and return it .
  3. Store that string value in a variable output.
  4. Display the result .

Program :

import java.util.Scanner;

public class Main

{

    public static void main(String[] args)
    {
        // creating scanner object
        Scanner sc = new Scanner(System.in);
        // input adouble value through scanner class
        System.out.println("Enter a Double value : ");
        double input1=sc.nextDouble();
        // converting to string
        String output =String.format("%f", input1);;
        System.out.println("Converted String is :"+output);
    }
}
Output : 

Enter a Double value : 131.123456 
Converted String is : 131.123456

Don’t stop learning now. Get hold of all the important Java fundamentals with the Simple java program example guide and practice well.

Related Java Program: