You can try BigDecimal for this purpose

Double toBeTruncated = new Double("3.5789055");

Double truncatedDouble = BigDecimal.valueOf(toBeTruncated)
    .setScale(3, RoundingMode.HALF_UP)
    .doubleValue();
Answer from Neel on Stack Overflow
🌐
TheServerSide
theserverside.com › blog › Coffee-Talk-Java-News-Stories-and-Opinions › Java-double-precision-2-decimal-places-example-float-range-math-jvm
Java double decimal precision
The precision of a double in Java is 10-324 decimal places, although true mathematical precision can suffer due to issues with binary arithmetic.
🌐
GeeksforGeeks
geeksforgeeks.org › java › how-to-set-precision-for-double-values-in-java
How to Set Precision For Double Values in Java? - GeeksforGeeks
July 12, 2025 - We can use the format() method of the String class to format the decimal number to some specific format. ... // Java Program to Illustrate format() Method // of String class // Importing required classes import java.io.*; import java.lang.*; ...
🌐
Baeldung
baeldung.com › home › java › java numbers › how to round a number to n decimal places in java
How to Round a Number to N Decimal Places in Java | Baeldung
September 24, 2025 - Internally, it might be stored as slightly less than 260.775, so rounding it to two decimal places results in 260.77 instead of 260.78. These inaccuracies stem from how floating-point numbers are stored in memory. Floating-point values are stored as a combination of a mantissa and an exponent, which allows them to represent a wide range of numbers but at the cost of precision.
🌐
Coderanch
coderanch.com › t › 509676 › java › Precision-scale-decimal
Precision and scale of a decimal value (Java in General forum at Coderanch)
In java decimal means a double value for a precision of 1 , and a scale of 7 , I am checking it this way : The max value of a double with precision 1 and scale 7 is 9.9999999, the reason I am doing this is anything above this precision and scale it should allow, but if it is below precision ...
🌐
Coderanch
coderanch.com › t › 383318 › java › increase-decimal-precision
How to increase decimal precision (Java in General forum at Coderanch)
Hi all, I've a question regarding precision for the float/double numbers in java. For example: how to increase the precision for say the number 1.2 to 1.20 or 2.300 to 2.3000 so... Thanks, Ravi. ... You cannot "increase the precision" of float and double. However, your most likely don't really mean that you want to increase the precision - you just want to know how to print a float or double with more decimal digits.
🌐
Tutorialspoint
tutorialspoint.com › java › math › bigdecimal_precision.htm
BigDecimal Precision in Java
The following example shows the usage of math.BigDecimal.precision() method. package com.tutorialspoint; import java.math.*; public class BigDecimalDemo { public static void main(String[] args) { // create 2 BigDecimal Objects BigDecimal bg1, bg2; bg1 = new BigDecimal("123.234"); bg2 = new BigDecimal("523"); // create 2 int objects int i1, i2; // assign the result of precision of bg1, bg2 to i1 and i2 i1 = bg1.precision(); i2 = bg2.precision(); String str1 = "The precision of " + bg1 + " is " + i1; String str2 = "The precision of " + bg2 + " is " + i2; // print the values of i1, i2 System.out.println( str1 ); System.out.println( str2 ); } }
🌐
Codersarts
codersarts.com › forum › java-programming-help › java-programming-help-how-to-set-precision-for-double-value
Java Programming Help: How to set Precision for double value | Codersarts
August 10, 2019 - You can set Precision for double value either via BigDecimal or DecimalFormat, depending on what you want to do with the value later. ... package com.codersarts; import java.text.DecimalFormat; public class DecimalFormatExample { // setting precision upto 2 decimal point private static ...
Find elsewhere
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › math › BigDecimal.html
BigDecimal (Java Platform SE 8 )
October 20, 2025 - ... Returns the signum function of this BigDecimal. ... Returns the scale of this BigDecimal. If zero or positive, the scale is the number of digits to the right of the decimal point. If negative, the unscaled value of the number is multiplied by ten to the power of the negation of the scale.
🌐
Reddit
reddit.com › r/java › java double precision
r/java on Reddit: Java Double Precision
August 24, 2020 -

I came across a piece of code in a legacy Java 8 application at work which adds two doubles and gives out a double. I observed that the resulting doubles for various inputs had variable number of digits after the decimal point. Some were very precise with 12 digits after the decimal point and some had merely a digit after the decimal point.

I’m curious to know what factors affect certain doubles to be so very precise and certain doubles not as much.

Examples:

double one = 3880.95; double two = 380.9; Result: 4261.849999999999

double one = 1293.65; double two = 1293.6; Result: 2587.25

🌐
The Floating-Point Guide
floating-point-gui.de › languages › java
The Floating-Point Guide - Floating-point cheat sheet for Java
Java has IEEE 754 single and double precision types supported by keywords: float f = 0.1f; // 32 bit float, note f suffix double d = 0.1d; // 64 bit float, suffix optional · The strictfp keyword on classes, interfaces and methods forces all intermediate results of floating-point calculations ...
Top answer
1 of 16
896

Use setRoundingMode, set the RoundingMode explicitly to handle your issue with the half-even round, then use the format pattern for your required output.

Example:

DecimalFormat df = new DecimalFormat("#.####");
df.setRoundingMode(RoundingMode.CEILING);
for (Number n : Arrays.asList(12, 123.12345, 0.23, 0.1, 2341234.212431324)) {
    Double d = n.doubleValue();
    System.out.println(df.format(d));
}

gives the output:

12
123.1235
0.23
0.1
2341234.2125

EDIT: The original answer does not address the accuracy of the double values. That is fine if you don't care much whether it rounds up or down. But if you want accurate rounding, then you need to take the expected accuracy of the values into account. Floating point values have a binary representation internally. That means that a value like 2.7735 does not actually have that exact value internally. It can be slightly larger or slightly smaller. If the internal value is slightly smaller, then it will not round up to 2.7740. To remedy that situation, you need to be aware of the accuracy of the values that you are working with, and add or subtract that value before rounding. For example, when you know that your values are accurate up to 6 digits, then to round half-way values up, add that accuracy to the value:

Double d = n.doubleValue() + 1e-6;

To round down, subtract the accuracy.

2 of 16
537

Assuming value is a double, you can do:

(double)Math.round(value * 100000d) / 100000d

That's for 5 digits precision. The number of zeros indicate the number of decimals.

🌐
Oracle
docs.oracle.com › javase › 7 › docs › api › java › math › BigDecimal.html
BigDecimal (Java Platform SE 7 )
... Returns the signum function of this BigDecimal. ... Returns the scale of this BigDecimal. If zero or positive, the scale is the number of digits to the right of the decimal point. If negative, the unscaled value of the number is multiplied by ten to the power of the negation of the scale.
🌐
TheServerSide
theserverside.com › blog › Coffee-Talk-Java-News-Stories-and-Opinions › Format-double-Java-printf-example
How to format a Java double with printf example
To use Java printf to format double and float values with decimal place precision, follow these two rules: Use %f as the double specifier in the text string.
🌐
Medium
solutionsarchitecture.medium.com › mastering-big-decimals-in-java-understanding-implementation-performance-and-alternatives-7e3a3a96efc6
Mastering Big Decimals in Java: Understanding Implementation, Performance, and Alternatives | by Rahul Krishnan | Medium
September 22, 2024 - In Java, BigDecimal is a class designed to handle arbitrary-precision decimal numbers. Unlike the primitive data types float and double, which have fixed precision, BigDecimal can represent numbers with any desired level of accuracy.
🌐
TutorialsPoint
tutorialspoint.com › precision-on-a-number-format-in-java
Precision on a number format in Java
Now, we want 3 decimal places. For that, use 1.3f − ... import java.util.Formatter; public class Demo { public static void main(String args[]) { Formatter f1, f2, f3; f1 = new Formatter(); f1.format("%1.3f", 29292929.98765432); System.out.println(f1); f2 = new Formatter(); f2.format("%1.7f", 29292929.98765432); System.out.println(f2); f3 = new Formatter(); f3.format("%1.9f", 29292929.98765432); System.out.println(f3); } }
🌐
Javacodemonk
javacodemonk.com › set-precision-and-scale-for-a-double-value-in-java-9c8d56f4
Precision and scale for a Double in java
August 3, 2020 - If you still want to lose the precision simply divide the number by 10 to the power precision. There are multiple ways in Java to round the double value to certain scale, as mentioned in the below example, ... import java.math.BigDecimal; import java.math.RoundingMode; import java.text.DecimalFormat; public class RoundDouble { public double round1(double input, int scale) { BigDecimal bigDecimal = new BigDecimal(input).setScale(scale, RoundingMode.HALF_EVEN); return bigDecimal.doubleValue(); } public double round2(double input) { return Math.round(input * 100) / 100.0d; } public double round3(
🌐
ZetCode
zetcode.com › java › bigdecimal
Java BigDecimal - high-precision calculations in Java with BigDecimal
In our case, we round the value to one decimal place with a rounding mode RoundingMode.FLOOR, which rouds towards negative infinity. ... With the compareTo method, two BigDecimal objects that are equal in value but have a different scale (like 4.0 and 4.00) are considered equal by this method. ... import java.math.BigDecimal; void main() { var x = new BigDecimal("1.6"); var y = new BigDecimal("1.60"); System.out.println(x.equals(y)); System.out.println(x.compareTo(y)); }