# Truncate Double in Java

Rupam Yadav Jan 30, 2023 Mar 15, 2021

In this article, we will look at how to truncate or round off a number to n decimal points in Java. Java provides two primitive types to store decimal values which are double and float. It is recommended to use the `BigDecimal` class for precise values or rounding off.

## Use `DecimalFormat` to Truncate Double in Java

`DecimalFormat` is a subclass of `NumberFormat` that can format decimal numbers using the formatting pattern provided. Here in the code below, we created an object of `DecimalFormat` passing a suitable formatting pattern. We can call the `format()` method on the `decimatFormat` object and pass our double variable `d` as a parameter.

The `format()` method returns a string that is truncated, as shown in the output.

``````import java.text.DecimalFormat;
public class DecimalFormatting {
public static void main(String args[]){
double d = 9.4568982982989;
String pattern = "#.###";
DecimalFormat decimalFormat =  new DecimalFormat(pattern);
String formattedDouble = decimalFormat.format(d);
System.out.println("Formatted double d = "+formattedDouble);
}
}
``````

Output:

``````Formatted double d = 9.457
``````

## Use `BigDecimal` to Truncate Double in Java

In Java, `BigDecimal` are immutable, arbitrary-precision signed decimal numbers. It provides control over scale and precision. We create a `BigDecimal` object passing the double converted as a string, preventing issues with representing approximate values.

We then call the `setScale(int newScale, RoundingMode roundingMode)` method on this `BigDecimal` object `bd`, which takes two arguments: the scale of BigDecimal to be returned and the other is the rounding mode to apply. The mode `RoundingMode.FLOOR` rounds towards negative infinity.

``````public class DecimalFormatting {
public static void main(String args[]){
double d = 9.4568982982989;
BigDecimal bd = new BigDecimal(String.valueOf(d));
BigDecimal rounded = bd.setScale(2,RoundingMode.FLOOR);
System.out.println("BigDecimal rounded off : "+rounded);
}
}
``````

Output:

``````BigDecimal rounded off : 9.45
``````

## Use Apache Common Library to Truncate Decimal in Java

We can achieve the same by this library whose maven dependency is given below.

``````<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-math3</artifactId>
<version>3.5</version>
</dependency>
``````

We can use the `Precision.round()` method from this library to format decimal. This method takes two arguments, one the decimal variable to be formatted and scale. We need 4 digits after the decimal point; hence that’s the scale we need. The output shows digits in the decimal are truncated.

``````import org.apache.commons.math3.util.Precision;
public class DecimalFormatting {
public static void main(String args[]){
double d = 9.4568982982989;
double doubleP = Precision.round(d,4);
System.out.println("Precision rounded off : "+doubleP);
}
}
``````

Output:

``````Precision rounded off : 9.4569
``````

## Use `java.lang.Math` to Truncate Decimal in Java

This is another way to round off decimal numbers in Java. The method `Math.pow()` controls `n` number of decimal places by multiplying and dividing by 10n. We create a double variable `scale` using this method which takes two arguments, base 10 and power 3.

`Math.round()` in Java is used to round a number to its closest integer. This is done by adding 1/2 to the number, talking the floor of the result, and casting the result to an integer data type. This method is not recommended as it sometimes truncates the value incorrectly.

``````public class DecimalFormatting {
public static void main(String args[]){
double d = 9.4568982982989;
double scale = Math.pow(10, 3);
double doubleM1 = Math.round(d*scale)/scale;
System.out.println("Math1 Rounded off: "+doubleM1);
}
}
``````

Output:

``````Math Rounded off: 9.45
Math1 Rounded off: 9.457
``````