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 - Similar to previous examples, we’ll write a helper method: public static double withDecimalFormatLocal(double value) { DecimalFormat df = (DecimalFormat) NumberFormat.getNumberInstance(Locale.getDefault()); return new Double(df.format(value)); } ...
🌐
GeeksforGeeks
geeksforgeeks.org › java › numberformat-class-java
NumberFormat Class in Java - GeeksforGeeks
January 23, 2026 - import java.text.NumberFormat; public class GFG { public static void main(String[] args) { NumberFormat nf = NumberFormat.getInstance(); System.out.println(nf.format(1234567.89)); } }
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › text › NumberFormat.html
NumberFormat (Java Platform SE 8 )
July 21, 2026 - For example in the English locale, with ParseIntegerOnly true, the string "1234." would be parsed as the integer value 1234 and parsing would stop at the "." character. Of course, the exact format accepted by the parse operation is locale dependant and determined by sub-classes of NumberFormat.
🌐
ZetCode
zetcode.com › java › numberformat
Java NumberFormat - formatting numbers and currencies in Java
The NumberFormat.getPercentInstance is used to format percentages. ... import java.text.NumberFormat; import java.util.Locale; void main() { double x = 25f / 100f; NumberFormat pf = NumberFormat.getPercentInstance(Locale.of("sk", "SK")); System.out.println(pf.format(x)); }
🌐
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.
🌐
Oracle
docs.oracle.com › javase › tutorial › java › data › numberformat.html
Formatting Numeric Print Output (The Java™ Tutorials > Learning the Java Language > Numbers and Strings)
The example that follows creates a DecimalFormat object, myFormatter, by passing a pattern string to the DecimalFormat constructor. The format() method, which DecimalFormat inherits from NumberFormat, is then invoked by myFormatter—it accepts a double value as an argument and returns the formatted ...
🌐
Oracle
docs.oracle.com › javase › tutorial › i18n › format › numberFormat.html
Using Predefined Formats (The Java™ Tutorials > Internationalization > Formatting)
The material that follows demonstrates formatting techniques with a sample program called NumberFormatDemo.java. You can use the NumberFormat methods to format primitive-type numbers, such as double, and their corresponding wrapper objects, such as Double. The following code example formats ...
🌐
GitHub
gist.github.com › dhatch › 2520974
Example of Java number formatting options · GitHub
Example of Java number formatting options · Raw · NumberFormattingExample.java · This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Find elsewhere
🌐
University of Auckland
cs.auckland.ac.nz › references › java › java1.5 › tutorial › i18n › format › numberFormat.html
Using Predefined Formats (The Java™ Tutorials > Internationalization > Formatting)
You can use the NumberFormat methods to format primitive-type numbers, such as double, and their corresponding wrapper objects, such as Double. The following code example formats a Double according to Locale. Invoking the getNumberInstance method returns a locale-specific instance of NumberFormat.
🌐
Java67
java67.com › 2015 › 06 › how-to-format-numbers-in-java.html
How to format numbers in Java? - NumberFormat Example | Java67
By default, NumberFormat uses a grouping size of three, so you will see a comma (grouping separator) after every three digits on the integer portion of the number starting from the right. If you want to use a custom grouping size, you need to use DecimalFormat class. It provides a method called setGroupingSize(int size) which can customize grouping size. Here is our Java program to add commas into numbers by using NumberFormat and DecimalFormat class :
🌐
TutorialsPoint
tutorialspoint.com › numberformat-class-in-java
Java Internationalization - NumberFormat Class
January 2, 2020 - 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 › 25 › docs › api › java.base › java › text › NumberFormat.html
NumberFormat (Java SE 25 & JDK 25)
July 21, 2026 - To get a NumberFormat for the default Locale, use one of the static factory methods that return a concrete subclass of NumberFormat. The following formats all provide an example of formatting the Number "2000.50" with the US locale as the default locale.
🌐
Thecoderscorner
thecoderscorner.com › team-blog › java-and-jvm › stream-io › 16-formatting-numbers-using-numberformat
Formatting numbers using NumberFormat
NumberFormat can also be configured manually with appropriate settings, as follows in this example: package com.thecoderscorner.example; import java.text.NumberFormat; public class Formatting { public static void main(String[] args) { // we create a number formatter for integers with no grouping int integerVal = 100; NumberFormat fmt = NumberFormat.getInstance(); fmt.setGroupingUsed(false); // dont want to group 1,000's fmt.setMaximumFractionDigits(0); // no fractions - integer only fmt.setMinimumFractionDigits(0); // as above String output = fmt.format(integerVal); System.out.println(output); // and now for a double with grouping and 3dp.
🌐
Runestone Academy
runestone.academy › ns › books › published › javajavajava › ch5-numformat.html
5.8 From the Java Library java.text.NumberFormat
For example, the following expression calculates maturity value with annual compounding: principal * Math.pow(1 + rate, years) ... Using a development environment that allows user input, design a command-line user interface to the BankCD class that lets the user input principal, interest rate, ...
🌐
Java Code Geeks
examples.javacodegeeks.com › home › java development › core java › text
Format number with custom NumberFormat - Java Code Geeks
May 11, 2013 - package com.javacodegeeks.snippets.core; import java.text.DecimalFormat; import java.text.NumberFormat; public class FormatNumberWithCustomNumberFormat { public static void main(String[] args) { NumberFormat formatter; String number; // 0 --> a digit or 0 if no digit present formatter = new DecimalFormat("00000"); number = formatter.format(-1234.567); System.out.println("Number 1: " + number); formatter = new DecimalFormat("0000.000"); number = formatter.format(-1234.567); System.out.println("Number 2: " + number); // # --> a digit or nothing if no digit present formatter = new DecimalFormat("##"); number = formatter.format(-1234.567); System.out.println("Number 3: " + number); formatter = new DecimalFormat("##00"); number = formatter.format(0); System.out.println("Number 4: " + number); // .
🌐
Carnegie Mellon University
cs.cmu.edu › afs › cs › academic › class › 15212-s98 › www › java › tutorial › intl › datamgmt › numbers.html
How to Format Numbers
Here's the code that AroundTheWorld uses to format it's three numbers: the GDP, the population, and the literacy rate for the three locales: gdpFormatter = NumberFormat.getCurrencyInstance(currentLocale); gdp = (Double) numbers.getObject("GDP"); gdpValue.setText(gdpFormatter.format(gdp)); .
🌐
Princeton
cs.princeton.edu › courses › archive › fall97 › cs461 › jdkdocs › api › java.text.NumberFormat.html
Class java.text.NumberFormat
NumberFormat nf = NumberFormat.getInstance(); for (int i = 0; i < a.length; ++i) { output.println(nf.format(myNumber[i]) + "; "); } To format a number for a different Locale, specify it in the call to getInstance.