As @Benoit already said in a comment, to keep the full precision of your number, you need a BigDecimal:

BigDecimal hashValue = new BigDecimal("20790123833965.960938");
DecimalFormat formatter = new DecimalFormat("#0.000000");
System.out.println(formatter.format(hashValue));

Output:

20790123833965.960938

Answer from Anonymous on Stack Overflow
🌐
Quora
quora.com › I-want-to-display-a-floating-number-in-Java-upto-6-decimal-places-such-number-is-not-rounded-off-How-do-I-do-so
I want to display a floating number in Java upto 6 decimal places such number is not rounded off. How do I do so? - Quora
Answer: Best & easy way to do formatting with floating numbers in JAVA is to do through [code ]DecimalFormat[/code] class. [code]DecimalFormat df = new DecimalFormat("#.######"); //Type 6 '#' after decimal to format the number to 6 decimal places. ...
🌐
CopyProgramming
copyprogramming.com › howto › java-output-till-6-decimal-places-in-java
Java: Printing Java Output with Precision up to 6 Decimal Places
May 20, 2023 - The resulting rounded value is stored in a variable named ' rounded = (double ', which is obtained by multiplying the original value by 1000000, rounding it to the nearest integer using the 'Math.round()' method, and then dividing it by 1000000 to obtain the final rounded value.
Top answer
1 of 16
897

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.

Top answer
1 of 2
3

I'm not sure I fully understand your question so let me recap:

  • You have some double value with potentially more than 6 significant fraction digits.
  • You want to round that value to 6 fraction digits and use the HALF_UP mode.
  • You want to display only the first 2 fraction digits (I don't understand why but I won't question it).

In that case I'd suggest you first use BigDecimal for the rounding since otherwise you could get precision errors:

double value = 1.23456789;
BigDecimal rounded = BigDecimal.valueOf( value ).setScale(6, RoundingMode.HALF_UP);

That should result in the value 1.234568.

Then use NumberFormat like you did but always round down:

DecimalFormat df = (DecimalFormat)DecimalFormat.getInstance(Locale.US);
df.applyPattern("#.##");
df.setRoundingMode(RoundingMode.DOWN);
String output = df.format( rounded );

That should result in 1.23.

With your input value of 4.23499999999235 you'd then get 4.235000 (rounded to 6 digits) and 4.23.

Edit: I'm not aware of any rounding mode that rounds 4.23499999999235 to 4.24 but you should be able to achieve it by chaining multiple setScale() calls:

BigDecimal rounded = BigDecimal.valueOf( value );
for( int digit = 6; digit >= 2; digit--) {
  rounded = rounded .setScale( digit, RoundingMode.HALF_UP );
}

That should apply rounding to fraction digit 6, then 5 etc. and finally round to 4.24. I'm still not sure that's the right way to round but you know your requirements better than me :)

2 of 2
0

Considering you are outputting this to a string, why not use

String output = String.format(java.util.Locale.US,"%.2f", doubleValue);

instead of all of the DecimalFormat stuff? This will leave doubleValue as the original value, keeping the precision.

🌐
Coderanch
coderanch.com › t › 681508 › java › maximum-decimal-places-float-double
The maximum decimal places for a float and a double: Where are they exactly defined? (Beginning Java forum at Coderanch)
Note that printing and storing the value are two separate concerns. What method did you use to print the value? System.out.println() or System.out.printf()? The former will use String.valueOf() to format the output of a float/double value. The latter will allow you to control how many decimal places you display.
🌐
Programiz
programiz.com › java-programming › examples › round-number-decimal
Java Program to Round a Number to n Decimal Places
So, 1.34567 rounded to 3 decimal places prints 1.346, 6 is the next number for 3rd place decimal 5.
Find elsewhere
🌐
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 - In Java, there are numbers more than 16 numbers only to the precision but can go more. Here we are given a double value, the task is to set its precision value to specific decimal places. It is illustrated below illustrations as follows: Input : val = 1 Output : 1.0000 Upto 4 decimal places · Input : 12.5 Output : 12.500000 Upto 6 decimal places
🌐
Edureka Community
edureka.co › home › community › categories › java › how can we display an output of float data with 2...
How can we display an output of float data with 2 decimal places in java Please help | Edureka Community
June 4, 2018 - 5865/display-output-float-data-with-decimal-places-java-please-help ... How can we display an output of float data with 2... how do I turn a double in an array from 1000 to infinity Jul 9, 2024 · I have created a code in java for a flower shop and now my IDE is showing all the inputs are in red showing there is an error Jul 6...
🌐
Java67
java67.com › 2014 › 06 › how-to-format-float-or-double-number-java-example.html
5 Examples of Formatting Float or Double Numbers to String in Java | Java67
In JDK 1.5, a new static method format() was added to the String class, which is similar to printf(), but returns a String. By the way there are numerous way to format numbers in Java, you can use either DecimalFormat class, or NumberFormat or even Formatter class to format floating point numbers in Java. Coming back to String's format method, here is a quick example of using it : String strDouble = String.format("%.2f", 1.23456); This will format the floating point number 1.23456 up-to 2 decimal places, because we have used two after decimal point in formatting instruction %.2f, f is for floating point number, which includes both double and float data type in Java.
🌐
Coderanch
coderanch.com › t › 625840 › java › Printing-number-digits-decimal-places
Printing number of digits after decimal places using print function (Beginning Java forum at Coderanch)
You want 16 for the places after the decimal point, 1 for the decimal point and two before the decimal point making .16f Add %n so you get a new line at the end of the output. You realise that a double is only precise to about 15.9 decimal places, so you are guaranteed inaccurate output if you try to print 18 digits, as in .16f? ... Campbell Ritchie wrote:Never import java.lang.Math.
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-program-to-round-a-number-to-n-decimal-places
Java Program to Round a Number to n Decimal Places - GeeksforGeeks
November 9, 2020 - import java.text.DecimalFormat; public class GFG { public static void main(String[] args) { double number = 145.34567; DecimalFormat df = new DecimalFormat("#.###"); System.out.println(df.format(number)); } }
🌐
Java2Blog
java2blog.com › home › number › format double to 2 decimal places in java
Format Double to 2 Decimal Places in Java [ 7 Ways ] - Java2Blog
November 8, 2023 - Learn how to format double values to 2 decimal places in Java using DecimalFormat, String’s format() method, System.out.printf() method, and BigDecimal class. Find out the advantages and disadvantages of each approach and how to use RoundingMode for different scenarios.
🌐
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
Formatting output in Java doesn't have to be hard. Here's how to use Java printf with double values to control decimal precision, thousands groupings and character width when you format ...
🌐
Coderanch
coderanch.com › t › 410238 › java › formatting-decimal-places
formatting to 4 decimal places (Beginning Java forum at Coderanch)
. . . String.format("%6.3f", doubleNumber); ... public class formatdemo { /** * @param args */ public static void main(String[] args) { Float myNumber = new Float(4.995); String myValue = String.format("%1.3f", myNumber); System.out.println(myValue); } } o/p - 4.995 Can't it roundoff the no. ... Originally posted by Dinesh Tahiliani: Can't it roundoff the no. If you pass a number with 3 decimal places and print it with 3 decimal places there won't be any rounding.