How to convert Double to String in Java
A string is a sequence of characters in computer programming that may be used as a literal constant or as a variable. The latter may allow for the mutation of its elements and the modification of their length. Or it may be fixed (after creation). A string is a data type that is frequently implemented as an array data structure of bytes (or words) that records a succession of items, most commonly characters, using some character encoding. More generic arrays or other sequences (or list) data types and structures can also be represented using a string.
The double data type, also known as double, is a data type in the JAVA programming language that aids in the storage of high-precision floating-point numbers or data in computer memory. This data type is also known as double because it can hold twice the amount of information and data as a float type. So today let us start with How to convert Double to String in Java.
What’s The Approach?
- Import the
java. util
package into the class.
- Now within the main method create a new object of
Scanner
class, for example,Scanner sc=new Scanner(System. in);
- Use the
Scanner
class to get the input from the user.
- Implement
Double.toString(double_variable);
to convert the double to String type.
Also Read: How to Reverse a Number in Java using while loop.
Java Program to convert Double to String in Java
/* * TechDecode Tutorials * * How to Covert Double to String * */ import java.util.*; public class Double_to_String { public static void main(String args[]) { // Creating an object of Scanner class Scanner sc= new Scanner(System.in); // taking input from the user System.out.print("Enter the Double value: "); double num=sc.nextDouble(); // converting double to string String s = Double.toString(num); System.out.println("String is = " + s); } }
Output:-