From this thread, there are different ways to do this:

double r = 5.1234;
System.out.println(r); // r is 5.1234

int decimalPlaces = 2;
BigDecimal bd = new BigDecimal(r);

// setScale is immutable
bd = bd.setScale(decimalPlaces, BigDecimal.ROUND_HALF_UP);
r = bd.doubleValue();

System.out.println(r); // r is 5.12

f = (float) (Math.round(n*100.0f)/100.0f);

DecimalFormat df2 = new DecimalFormat( "#,###,###,##0.00" );
double dd = 100.2397;
double dd2dec = new Double(df2.format(dd)).doubleValue();

// The value of dd2dec will be 100.24

The DecimalFormat() seems to be the most dynamic way to do it, and it is also very easy to understand when reading others code.

Answer from Espo on Stack Overflow
🌐
Baeldung
baeldung.com › home › java › java numbers › number formatting in java
Number Formatting in Java | Baeldung
August 9, 2024 - DecimalFormat is one of the most popular ways to format a decimal number in Java.
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › text › NumberFormat.html
NumberFormat (Java Platform SE 8 )
October 20, 2025 - Java™ Platform Standard Ed. 8 ... NumberFormat is the abstract base class for all number formats. This class provides the interface for formatting and parsing numbers.
Discussions

[Java] DecimalFormat vs NumberFormat?
According to the documentation ( https://docs.oracle.com/javase/7/docs/api/java/text/NumberFormat.html ), NumberFormat is an abstract base class for number formatting; DecimalFormat is a concrete derived class. But I think these are more about locale-specific formatting (commas or spaces between thousands etc). Are you just saying you want to display a double with 5 decimal places? More on reddit.com
🌐 r/learnprogramming
6
2
February 28, 2018
What was the thought process behind the design of the NumberFormat class? Why doesn't it have a way to set a pattern?
The real question is what was the thought process behind not making all of these threads safe. More on reddit.com
🌐 r/java
16
15
July 5, 2024
Phone number breakdown (java problem) - How should I do it?
The most simple solution for you is the following: There is no need for loops and regexes. In this case you should: Convert the initial phone number to string. (Telephone numbers are strings of digit characters, they are not integers! There are no arithmetics using phone numbers etc) change variables to string too. replace the % 1000 stuff with String.substring(int beginIndex, int endIndex) method. This is way easier than what you previously did. More on reddit.com
🌐 r/javahelp
10
3
August 21, 2020
question: How to format digits into dollar amounts
"never" use doubles for money. you lose precision. it is impossible to represent "0.1" as a float type number with a finite number of bits. http://javarevisited.blogspot.com/2012/02/java-mistake-1-using-float-and-double.html that being said, you can use DecimalFormat or the printf family of functions introduced in Java 5. They should work with float-types and BigDecimal; but it's been a while since I looked at it so the details are left as an exercise.... yada yada. More on reddit.com
🌐 r/java
6
3
September 11, 2012
🌐
Coderanch
coderanch.com › t › 381807 › java › Format-number-string-include-leading
Format number string to include leading zeros (Java in General forum at Coderanch)
January 8, 2007 - For your problem as described, Integer.parseInt() would be a better fit - but the idea is the same. And if you're using JDK 5+, you can use a format string to make this a little more compact: You may also want to ask yourself: what if the user enters something invalid in the text field?
🌐
IIT Kanpur
iitk.ac.in › esc101 › 05Aug › tutorial › java › data › decimalFormat.html
Formatting Numbers with Custom Formats
You can use the DecimalFormat class to format decimal numbers into strings. This class allows you to control the display of leading and trailing zeros, prefixes and suffixes, grouping (thousands) separators, and the decimal separator. If you want to change formatting symbols, such as the decimal ...
🌐
GeeksforGeeks
geeksforgeeks.org › java › numberformat-class-java
NumberFormat Class in Java - GeeksforGeeks
January 23, 2026 - Displays numbers in a clear, user-friendly, and locale-aware format ... import java.text.NumberFormat; public class GFG { public static void main(String[] args) { NumberFormat nf = NumberFormat.getInstance(); System.out.println(nf.format(12...
Find elsewhere
🌐
Oracle
docs.oracle.com › javase › tutorial › java › data › numberformat.html
Formatting Numeric Print Output (The Java™ Tutorials > Learning the Java Language > Numbers and Strings)
You can use the java.text.DecimalFormat class to control the display of leading and trailing zeros, prefixes and suffixes, grouping (thousands) separators, and the decimal separator. DecimalFormat offers a great deal of flexibility in the formatting of numbers, but it can make your code more complex.
🌐
Tutorialspoint
tutorialspoint.com › java › java_compact_number_formatting.htm
Java - Compact Number Formatting
Java 12 introduces compact formatting where we can format long numbers for decimals, currency, or percentages into short form or long form. For example 1000 to 1K. This is very useful where we've limited space or requirements to show numbers in short form like K for thousand, M for million B for Bil
🌐
Reddit
reddit.com › r/learnprogramming › [java] decimalformat vs numberformat?
r/learnprogramming on Reddit: [Java] DecimalFormat vs NumberFormat?
February 28, 2018 -

I find the area of an object and store it in a type double. If I want a specific number of decimal places, say 5, which one is better to use?

e.g. 4.55 > 4.55000,

But I guess maybe I need to understand the idea of decimal places. If a number is 4.555555, rounded to 5 decimal places, will it be 4.55556? If so, will the DecimalFormat be what I need to look into? I see if I do #.##### it will round down, but if I use zeroes, it will round normally like in math: #.00000.

If this is correct..can someone tell me the difference between number format and decimal format then? When to use each.

🌐
Medium
medium.com › @AlexanderObregon › formatting-numbers-for-display-with-decimalformat-in-java-0577c0fe0b52
Formatting Numbers for Display with DecimalFormat in Java
June 27, 2025 - Learn how DecimalFormat works to display numbers with commas, decimals, or currency symbols and how to use it safely with formatting logic in Java.
🌐
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. Precede the letter f with a decimal and number to specify precision.
🌐
W3Schools
w3schools.com › java › java_data_types_numbers.asp
Java Numbers
There are two types: float and double. Even though there are many numeric types in Java, the most used for numbers are int (for whole numbers) and double (for floating point numbers).
🌐
DaniWeb
daniweb.com › programming › software-development › threads › 205639 › put-comma-in-number-format
java - Put Comma In Number Format [SOLVED] | DaniWeb
Oracle documents that number formats are generally not synchronized; create separate instances per thread or guard with external synchronization. Oracle JDK 11 NumberFormat docs (Synchronization). ... Prefer passing an explicit Locale if you want a specific separator; relying on the default locale can surprise you when code moves machines. Avoid patterns that try to mix grouping sizes (for example, Indian-style "#,##,##0"). In java.text.DecimalFormat the grouping size is constant and, if you supply multiple grouping characters in the pattern, only the interval nearest the end is used.
🌐
W3Schools
w3schools.com › java › ref_string_format.asp
Java String format() Method
Java Wrapper Classes Java Generics Java Annotations Java RegEx Java Threads Java Lambda Java Advanced Sorting ... How Tos Add Two Numbers Swap Two Variables Even or Odd Number Reverse a Number Positive or Negative Square Root Area of Rectangle Celsius to Fahrenheit Sum of Digits Check Armstrong Num Random Number Count Words Count Vowels in a String Remove Vowels Count Digits in a String Reverse a String Palindrome Check Check Anagram Convert String to Array Remove Whitespace Count Character Frequency Sum of Array Elements Find Array Average Sort an Array Find Smallest Element Find Largest Element Second Largest Array Min and Max Array Merge Two Arrays Remove Duplicates Find Duplicates Shuffle an Array Factorial of a Number Fibonacci Sequence Find GCD Check Prime Number ArrayList Loop HashMap Loop Loop Through an Enum
🌐
Oracle
docs.oracle.com › en › java › javase › 11 › docs › api › java.base › java › text › NumberFormat.html
NumberFormat (Java SE 11 & JDK 11 )
January 20, 2026 - With this format, a fraction like 0.53 is displayed as 53%. You can also control the display of numbers with such methods as setMinimumFractionDigits. If you want even more control over the format or parsing, or want to give your users more control, you can try casting the NumberFormat you get from the factory methods to a DecimalFormat.
🌐
Oracle
docs.oracle.com › javase › tutorial › i18n › format › numberFormat.html
Using Predefined Formats (The Java™ Tutorials > Internationalization > Formatting)
This example prints the following; it shows how the format of the same number varies with Locale: 123 456 fr_FR 345 987,246 fr_FR 123.456 de_DE 345.987,246 de_DE 123,456 en_US 345,987.246 en_US · By default, when text contains numeric values, those values are displayed using Arabic digits. When other Unicode digit shapes are preferred, use the java.awt.font.NumericShaper class.
🌐
Codemia
codemia.io › knowledge-hub › path › how_to_format_a_java_string_with_leading_zero
How to format a Java string with leading zero?
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises
🌐
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
You can use the Java DecimalFormat class to define a format pattern that rounds numbers to a specific number of decimal places for display.
🌐
BeginnersBook
beginnersbook.com › 2017 › 10 › java-string-format-method
Java String format() method
June 9, 2024 - There are so many things you can do with this method, for example you can concatenate the strings using this method and, at the same time you can format the output of concatenated string. In this tutorial, we will see several examples of Java String format() method. String formattedString = String.format(String format, Object... args); %s – String · %d – Integer · %f – Floating point number ·