Use a DecimalFormatter:

double number = 0.9999999999999;
DecimalFormat numberFormat = new DecimalFormat("#.00");
System.out.println(numberFormat.format(number));

Will give you "0.99". You can add or subtract 0 on the right side to get more or less decimals.

Or use '#' on the right to make the additional digits optional, as in with #.## (0.30) would drop the trailing 0 to become (0.3).

Answer from Alex on Stack Overflow
๐ŸŒ
TutorialCup
tutorialcup.com โ€บ home โ€บ java tutorial โ€บ how to limit decimal places in java
How to limit decimal places in Java
October 11, 2021 - The Math.round() method is another method to limit the decimal places in Java. If we want to round a number to 1 decimal place, then we multiply and divide the input number by 10.0 in the round() method.
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ java โ€บ java numbers โ€บ truncate a double to two decimal places in java
Truncate a Double to Two Decimal Places in Java | Baeldung
September 24, 2025 - The โ€˜fโ€™ at the end of our format String instructs the formatter to produce a decimal format, and the โ€˜.2โ€™ means we want two digits after the decimal place. We could adjust this to truncate to the amount of decimal places required.
๐ŸŒ
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
A double in Java is a 64-bit number, and the 64-bit precision of a double never changes within a Java program. However, to format the output of a double to two decimal places, simply use the printf method and %.2f as the specifier.
๐ŸŒ
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.math.RoundingMode; ...ntln(df.format(number)); } } ... In this approach, we first Multiply the number by 10n using the pow() function of the Math class....
๐ŸŒ
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)
think of scientific notation: 4.232 x 10^3. if everyone always agrees that the base for the exponent is "10", we don't need to store that. we can just store the "3". now, if we know we have 10 places/bytes/digits to use to store this number, we can decide the first 6 are for the "number" part, and the last four are for the "exponent part". so the above becomes: 0042320003 Java does basically that...except...it uses base 2 for everything. so in a 32-bit type, it uses something like (I've not looked it up, so don't quote me on this) 20 bits for the "number", and 12 for the "exponent". So the answer to "how many decimal places" is in a way "it depends".
๐ŸŒ
Quora
quora.com โ€บ How-do-I-cut-off-decimals-in-Java
How to cut off decimals in Java - Quora
Answer (1 of 3): Use a [code ]double[/code], or a [code ]BigDecimal[/code] if more appropriate, to represent your actual number. Use [code ]NumberFormat[/code] to format it for โ€œhuman-readableโ€ display.
Find elsewhere
๐ŸŒ
Coderanch
coderanch.com โ€บ t โ€บ 394217 โ€บ java โ€บ Method-limiting-decimal-places
Method for limiting decimal places? (Beginning Java forum at Coderanch)
this forum made possible by our volunteer staff, including ... ... I'm using the double type within an equation and printing out the results. However, depending on the double types used within the equation, I can end up with several places to the right of the decimal point. Is there a method that will limit the number places to the right of a decimal point? Thanks for your time, ... You mean when you print the number out, yes? Look at java.text.DecimalFormat.
๐ŸŒ
Coderanch
coderanch.com โ€บ t โ€บ 719510 โ€บ java โ€บ restrict-decimal-place-setter-method
restrict decimal place in setter method (Java in General forum at Coderanch)
I have a method whose method signature i cant change it is public boolean setAcctBal(double acctbal) {} This method is my validation method that will only allow a number with the 2 decimal places after the . I want to check it is the tester class by using the method setAcctBal(9.50); and that should say true and if i use setAcctBal(9.5962); it would be false. How can I do this with this specific method as boolean with a double paramater? I think the main reasons I have to do is method is for learning to limit what can be changed outside of a class and keeping it realisitic.
๐ŸŒ
Quora
quora.com โ€บ How-do-I-set-decimal-places-in-Java
How to set decimal places in Java - Quora
Answer (1 of 2): In what context? What are you trying to do? If youโ€™re calculating floating point numbers, and just need to display them with a certain number of digits of precision, you can perform the calculations at full scale, and then round to whatever level of precision you need.
๐ŸŒ
Sentry
sentry.io โ€บ sentry answers โ€บ java โ€บ round a number to n decimal places in java
Round a Number to N Decimal Places in Java | Sentry
The result is a string with the value rounded to 2 decimal places. ... The String.format() method is easy and useful when you need to display formatted numbers, but like DecimalFormat, the result is a string and not suitable for further numerical calculations. To make the rounding process reusable, you can create a custom utility method that encapsulates one of the approaches shown above. ... import java.math.BigDecimal; import java.math.RoundingMode; public class Main { public static void main(String[] args) { double value = 12.34567; double roundedValue = roundToNDecimalPlaces(value, 2); System.out.println("Rounded value: " + roundedValue); } public static double roundToNDecimalPlaces(double value, int decimalPlaces) { BigDecimal bd = new BigDecimal(Double.toString(value)); bd = bd.setScale(decimalPlaces, RoundingMode.HALF_UP); return bd.doubleValue(); } }
๐ŸŒ
Quora
quora.com โ€บ How-can-I-shorten-the-decimal-places-in-Java
How to shorten the decimal places in Java - Quora
1. Use setRoundingMode, set the RoundingMode explicitly to handle your issue with the half-even round, then use the format pattern for your required output. 2. double roundOff = Math.round(a * 100.0) / 100.0; 3. double roundOff = (double...
๐ŸŒ
Mathbits
mathbits.com โ€บ JavaBitsNotebook โ€บ DataBasics โ€บ Money.html
Controlling Decimal Output - JavaBitsNotebook.com
Return to Unit Menu | JavaBitsNotebook.com | MathBits.com | Terms of Use | JavaMathBits.com Controlling Decimal Output ยท It is often necessary to print values with a designated number of decimal places. For example, when printing amounts of money, it is customary to have two decimal places ...
๐ŸŒ
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 - Input : 12.5 Output : 12.500000 Upto 6 decimal places ... 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 ...
๐ŸŒ
javaspring
javaspring.net โ€บ blog โ€บ how-to-limit-decimal-places-in-java
How to Limit Decimal Places in Java โ€” javaspring.net
Limiting decimal places in Java is a common task that can be accomplished using different methods. DecimalFormat and String.format() are convenient for simple formatting and display purposes, while BigDecimal is the best choice for high-precision arithmetic, especially in financial applications.
๐ŸŒ
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
It is a common requirement to format currencies to two decimal places. You can easily achieve this with the Java printf function. Just use %.2f as the format specifier.