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
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › text › NumberFormat.html
NumberFormat (Java Platform SE 8 )
July 21, 2026 - 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.
🌐
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.
🌐
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...
🌐
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.
🌐
ZetCode
zetcode.com › java › numberformat
Java NumberFormat - formatting numbers and currencies in Java
Different cultures use different ways to represent numbers. For instance, the way currencies are formatted is widely different in the countries around the world. NumberFormat is a Java class for formatting and parsing numbers.
🌐
Java67
java67.com › 2015 › 06 › how-to-format-numbers-in-java.html
How to format numbers in Java? - NumberFormat Example | Java67
So if grouping is your need and you are happy with a grouping size of three then go for NumberFormat but if you want advanced formatting and custom grouping size then go for DecimalFormat class. If you are a beginner in Java and interested in learning essential features e.g.
🌐
Jenkov
jenkov.com › tutorials › java-internationalization › numberformat.html
Java NumberFormat
June 23, 2014 - The java.text.NumberFormat class is used to format numbers according to a specific Locale. Different countries have different standards for how they format numbers. In Denmark fractions of a number are separated from the integer part using a comma.
Find elsewhere
🌐
IIT Kanpur
iitk.ac.in › esc101 › 05Aug › tutorial › java › data › numberintro.html
Formatting Numbers
DecimalFormat, to format primitive-type numbers, such as double, and their corresponding wrapper objects, such as Double. The NumberFormat and DecimalFormat classes are in the java.text package.
🌐
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 - NumberFormat helps you to format and parse numbers for any locale. Your code can be completely independent of the locale conventions for decimal points, thousands-separators, or even the particular decimal digits used, or whether the number format is even decimal.
🌐
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. The NumericShaper API enables you to display a numeric value represented internally as an ASCII value in any Unicode digit shape.
🌐
Oracle
docs.oracle.com › javase › 7 › docs › api › java › text › NumberFormat.html
NumberFormat (Java Platform SE 7 )
Java™ Platform Standard Ed. 7 ... NumberFormat is the abstract base class for all number formats. This class provides the interface for formatting and parsing numbers.
🌐
Runestone Academy
runestone.academy › ns › books › published › javajavajava › ch5-numformat.html
Java, Java, Java: Object-Oriented Problem Solving, 2024E
Fortunately, Java supplies the java.text.NumberFormat class precisely for the task of representing numbers as dollar amounts, percentages, and other formats (Figure 5.8.1). ... Figure 5.8.1. The NumberFormat class. ... The NumberFormat class is an abstract class, which means that it cannot be directly instantiated...
🌐
TheServerSide
theserverside.com › blog › Coffee-Talk-Java-News-Stories-and-Opinions › How-to-format-a-Java-int-with-printf-example
How to format a Java int or long with printf in Java 25
Learn how to format Java int, long, short and byte values with %d format specifiers, String.formatted and Java 25's IO class for clean console output.
Published: October 15, 2025
🌐
Medium
medium.com › @AlexanderObregon › formatting-numbers-for-display-with-decimalformat-in-java-0577c0fe0b52
Formatting Numbers for Display with DecimalFormat in Java
June 27, 2025 - It turns plain numbers into strings formatted with commas, decimal places, or even symbols like currency signs. Behind the scenes, it works off a flexible pattern-based system that parses, formats, and localizes output without changing the number’s actual value.
🌐
TutorialsPoint
tutorialspoint.com › java_i18n › java_i18n_numberformat.htm
Java Internationalization - NumberFormat Class
In this example, we're formatting numbers based on US locale and Danish Locale. import java.text.NumberFormat; import java.util.Locale; public class I18NTester { public static void main(String[] args) { Locale enLocale = new Locale("en", "US"); Locale daLocale = new Locale("da", "DK"); NumberFormat numberFormat = NumberFormat.getInstance(daLocale); System.out.println(numberFormat.format(100.76)); numberFormat = NumberFormat.getInstance(enLocale); System.out.println(numberFormat.format(100.76)); } }
🌐
Oracle
docs.oracle.com › en › java › javase › 17 › docs › api › java.base › java › text › NumberFormat.html
NumberFormat (Java SE 17 & JDK 17)
April 21, 2026 - For example, 2000 can be formatted as "2K" in US locale. Use getPercentInstance to get a format for displaying percentages. 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.
🌐
YouTube
youtube.com › watch
Number Formatting in Java - YouTube
Introduction to the DecimalFormat class and the NumberFormat class.
Published: September 2, 2020
🌐
Java
download.java.net › java › early_access › valhalla › docs › api › java.base › java › text › NumberFormat.html
NumberFormat (Java SE 23 & JDK 23 [build 1])
NumberFormat helps you to format and parse numbers for any locale. Your code can be completely independent of the locale conventions for decimal points, thousands-separators, or even the particular decimal digits used, or whether the number format is even decimal.