A double keyword is used to define a floating-point variable. It is a primitive type and uses 64-bits to store data. It represents double-precision values that provide a more accurate output when operating on large values numbers. Its default value is 0.0d.
By default, Java treats decimal value as double type.
Syntax:
double doubNum = 2.5;
Double supports the widest range of data and is a signed type that can hold positive and negative values. Below is the program to demonstrate the same.
public class DoubleExample { public static void main(String[] args) { double posNum = 2.5; double negNum = -2.5; System.out.println("Positive double :: "+ posNum); System.out.println("Negative double :: "+ negNum); } }
Output:
Positive double :: 2.5 Negative double :: -2.5
Double supports a wider range of values as compared to int. Therefore, it is a lossy conversion. There are multiple ways to convert a double datatype to an integer type.
Below are the following methods.
In this, downcasting comes into the picture. Java compiler simply cuts off the decimal part and assigns the rest to the int variable. It doesn’t perform any calculation or operation to achieve this. For example, 6.78 is the value, then result would be 6.
Java program for converting double to int using typecasting
public class DoubleExample { public static void main(String[] args) { double doubNum = 2.5; int intNum = (int) doubNum; System.out.println("using typecasting :: "+ intNum); } }
Output:
using typecasting :: 2
It’s a method that Double wrapper class provides. It returns an integer representation of the double value passed to the argument.
Java program for converting double to int using intValue
public class DoubleExample { public static void main(String[] args) { double doubNum = 2.5; int intNum = new Double(doubNum).intValue(); System.out.println("using intValue method :: "+ intNum); } }
Output :
using intValue method :: 2
There are various approaches to convert double to string in Java with the help of inbuilt methods. As we know, double supports widest value range in primitive types and converting it to an other type can cause data loss. But, string can store and represent the large values.
String class itself provides approaches to convert double such as format(), valueOf(), concatenation operator and more.
toString() is another method that returns string representation of an object. In this case, the method acts on primitive double value.
Java program for converting double to string using toString() method
public class DoubleExample { public static void main(String[] args) { double doubNum = 2.5434; String str = Double.toString(doubNum); System.out.println("using toString() :: "+ str); } }
Output:
using toString() :: 2.5434
This approach uses a concatenation operator to convert a double variable to a string. In this approach, an empty string is concatenated with the desired value that results in string type.
Java program for converting double to string using concatenation operator
public class DoubleExample { public static void main(String[] args) { double doubNum = 2.5434; String str = ""+doubNum; System.out.println("using concatenation operator :: "+ str); } }
Output:
using concatenation operator :: 2.5434
String.valueOf() method is a static method in the String class that accepts a single parameter ranging from int to Object and returns its string representation. Below is the program for the same.
Java program for converting double to string using valueOf() method
public class DoubleExample { public static void main(String[] args) { double doubNum = 2.5434; String str = String.valueOf(doubNum); System.out.println("using valueOf :: "+ str); } }
Output:
using valueOf :: 2.5434
It returns specified string object based on provided string format and object arguments.
Java program for converting double to string using format() method
public class DoubleExample { public static void main(String[] args) { double doubNum = 2.5434; String str = String.format("%.4f",doubNum); System.out.println("using format method :: "+ str); } }
Output:
using format method :: 2.5434
DecimalFormat class provides a method named format() that formats a number based on a pattern provided in the constructor. After which, the format method converts the respective number into provided patterns.
Java program for converting double to string using DecimalFormat
public class DoubleExample { public static void main(String[] args) { double doubNum = 2.5434; String str = new DecimalFormat().format(doubNum); System.out.println("using decimalFormat() :: "+ str); } }
Output:
using decimalFormat() :: 2.543
We can convert a double type to String using StringBuffer and StringBuilder classes. They provide mutable strings and use the append method to concatenate the desired value to the current character sequence.
Java program for converting double to string using StringBuilder and StringBuffer classes
public class DoubleExample { public static void main(String[] args) { double doubNum = 2.5434; StringBuffer stringBuffer = new StringBuffer(); StringBuilder stringBuilder = new StringBuilder(); stringBuilder.append(doubNum); stringBuffer.append(doubNum); System.out.println("using stringBuilder :: "+ stringBuilder); System.out.println("using stringBuffer :: "+ stringBuffer); } }
Output:
using stringBuilder :: 2.5434 using stringBuffer :: 2.5434
There are situations where we are sometimes required to convert a fractional number to an integral type. For this, there are various approaches for converting a double to a long type in Java.
Here is the way to convert a double to long in Java.
This approach downcasts a double value to long type. The resultant value provides a non-fractional value and cuts off decimal part.
Java program for converting double to long using typecasting
public class DoubleExample { public static void main(String[] args) { double doubNum = 2.5434; long longNum = (long) doubNum; System.out.println("using typecasting() :: "+ longNum); } }
Output:
using typecasting() :: 2
longValue() is a static method used to convert a double object to long type. In this approach, one needs to wrap a long primitive type to a long object in order to use longValue() method.
Java program for converting double to long using longValue() method
public class DoubleExample { public static void main(String[] args) { double doubNum = 2.5434; long longNum = new Double(doubNum).longValue(); System.out.println("using longValue() :: "+ longNum); } }
Output:
using longValue() :: 2
As the name suggests, the round() method is a static method that accepts double type and rounds the number to the nearest long literal.
Java program for converting double to long using round() method
public class DoubleExample { public static void main(String[] args) { double doubNum = 2.5434; long longNum =Math.round(doubNum); System.out.println("using round() :: "+ longNum); } }
Output:
using round() :: 3
1. What is double in Java ?
double is a 64-bit or 8 byte primitive data type that specifies double precision values to provide more precise value for storing floating point type.
2. What is the size and default value of double in Java ?
The size is 64-bit or 8 bytes and 0.0d is its default value.
This post was last modified on July 4, 2022