Write a Java Program to convert double type variables to string

Java Program to convert double type variables to string

Welcome to the World of Online Learning:

Hello Friends “This blog helps you to learn Java programming concepts. You can learn Java  language at your own speed and time. One can learn concepts of Java language by practicing various programs given on various pages of this blog. Enjoy the power of Self-learning using the Internet.”

Java Program to convert double type variables to string
Java Program to convert double type variables to string

Write a Java Program to convert double type variables to string

PROGRAM: Java Program to convert double type variables to string

/*Java Program to convert double type variables to string*/

1: Java Program to Convert double to string using valueOf()

class Main {
public static void main(String[] args) {

// create double variable
double num1 = 36.33;
double num2 = 99.99;

// convert double to string
// using valueOf()
String str1 = String.valueOf(num1);
String str2 = String.valueOf(num2);

// print string variables
System.out.println(str1); // 36.33
System.out.println(str2); // 99.99
}
}

2: Java Program to Convert double to string using toString()

class Main {
public static void main(String[] args) {

// create double variables
double num1 = 4.76;
double num2 = 786.56;

// convert double to string
// using toString()
String str1 = Double.toString(num1);
String str2 = Double.toString(num2);

// print string variables
System.out.println(str1); // 4.76
System.out.println(str2); // 786.56
}
}

3: Java Program to Convert double to String using + Operator

class Main {
public static void main(String[] args) {

// create double variables
double num1 = 347.6D;
double num2 = 86.56D;

// convert double to string
// using + sign
String str1 = “” + num1;
String str2 = “” + num2;

// print string variables
System.out.println(str1); // 347.6
System.out.println(str2); // 86.56
}
}

4: Java Program to Convert double to String using format()

class Main {
public static void main(String[] args) {

// create a double variable
double num = 99.99;

// convert double to string using format()
String str = String.format(“%f”, num);

System.out.println(str); // 99.990000
}
}

Leave a Reply

Your email address will not be published. Required fields are marked *