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 - In this tutorial, we’ll learn about the different approaches to number formatting in Java, and how to implement them. The String#format method is very useful for formatting numbers. The method takes two arguments. The first argument describes the pattern of how many decimals places we want to see, and the second argument is the given value:
🌐
Oracle
docs.oracle.com › javase › tutorial › i18n › format › decimalFormat.html
Customizing Formats (The Java™ Tutorials > Internationalization > Formatting)
The example that follows creates a formatter by passing a pattern String to the DecimalFormat constructor. The format method accepts a double value as an argument and returns the formatted number in a String:
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › text › NumberFormat.html
NumberFormat (Java Platform SE 8 )
July 21, 2026 - setDecimalSeparatorAlwaysShown : only affects formatting, and only where there might be no digits after the decimal point, such as with a pattern like "#,##0.##", e.g., if true, 3456.00 → "3,456." if false, 3456.00 → "3456" This is independent of parsing.
🌐
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 format() method, which DecimalFormat inherits from NumberFormat, is then invoked by myFormatter—it accepts a double value as an argument and returns the formatted number in a string: Here is a sample program that illustrates the use of DecimalFormat: import java.text.*; public class DecimalFormatDemo { static public void customFormat(String pattern, double value ) { DecimalFormat myFormatter = new DecimalFormat(pattern); String output = myFormatter.format(value); System.out.println(value + " " + pattern + " " + output); } static public void main(String[] args) { customFormat("###,###.###", 123456.789); customFormat("###.##", 123456.789); customFormat("000000.000", 123.78); customFormat("$###,###.###", 12345.67); } } The output is: 123456.789 ###,###.### 123,456.789 123456.789 ###.## 123456.79 123.78 000000.000 000123.780 12345.67 $###,###.### $12,345.67 ·
🌐
Baeldung
baeldung.com › home › java › java numbers › a practical guide to decimalformat
A Practical Guide to DecimalFormat | Baeldung
January 8, 2024 - When the pattern gets applied to a number, its formatting rules are executed, and the result is printed according to the DecimalFormatSymbol of our JVM’s Locale unless a specific Locale is specified.
🌐
Medium
medium.com › @AlexanderObregon › formatting-numbers-for-display-with-decimalformat-in-java-0577c0fe0b52
Formatting Numbers for Display with DecimalFormat in Java
June 27, 2025 - Formatting numbers for invoices, summaries, and reports is something nearly every Java application has to deal with at some point. Showing raw values like 1234567.8 to a user doesn’t work well. It looks messy, lacks separators, and doesn’t reflect currency or locale conventions. That’s where DecimalFormat comes in. 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.
🌐
IIT Kanpur
iitk.ac.in › esc101 › 05Aug › tutorial › java › data › decimalFormat.html
Formatting Numbers with Custom Formats
The format method accepts a double value as an argument and returns the formatted number in a String: DecimalFormat myFormatter = new DecimalFormat(pattern); String output = myFormatter.format(value); System.out.println(value + " " + pattern + " " + output); The output for the preceding lines ...
Find elsewhere
🌐
TutorialsPoint
tutorialspoint.com › java_i18n › java_i18n_format_patterns.htm
Java Internationalization - Formatting Patterns
In this example, we're formatting numbers based on different patterns. import java.text.DecimalFormat; public class I18NTester { public static void main(String[] args) { String pattern = "###.###"; double number = 123456789.123; DecimalFormat numberFormat = new DecimalFormat(pattern); System.out.println(number); //pattern ###.### System.out.println(numberFormat.format(number)); //pattern ###.# numberFormat.applyPattern("###.#"); System.out.println(numberFormat.format(number)); //pattern ###,###.## numberFormat.applyPattern("###,###.##"); System.out.println(numberFormat.format(number)); number = 9.34; //pattern 000.### numberFormat.applyPattern("000.##"); System.out.println(numberFormat.format(number)); } }
🌐
Jenkov
jenkov.com › tutorials › java-internationalization › decimalformat.html
Java DecimalFormat
June 23, 2014 - The pattern parameter passed to the DecimalFormat constructor is the number pattern that numbers should be formatted according to.
🌐
ZetCode
zetcode.com › java › numberformat
Java NumberFormat - formatting numbers and currencies in Java
Java NumberFormat tutorial shows how to format and parse numbers and currencies in Java. We set the number of fractional digits, round numbers, and group digits.
🌐
University of Pennsylvania
cis.upenn.edu › ~bcpierce › courses › 629 › papers › Java-tutorial › intl › datamgmt › numbers.html
How to Format Numbers
NumberFormat formatter = NumberFormat.getInstance(Locale.US); String result = formatter.format(-1234.56); System.out.println(result); Tweaking the settings in the demo applet corresponds to changing this basic Java code in various ways.
🌐
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
For console tutorials, reports and fixed-width output, Formatter patterns remain concise and powerful. For application interfaces intended for different locales, consider the locale-focused number-formatting APIs. For modern Java 25 examples, a particularly clean pattern is to use Formatter syntax through String.formatted() and send the result to IO.println().
Published: October 15, 2025
🌐
MIT
web.mit.edu › java › java_v1.1.6 › distrib › i386_rh9 › demo › i18n › NumberFormat › example1.html
Number Formatting Demo Guide
The number formatters: Provide control over every aspect of the display, including separator characters, optional digits, maximum and minimum decimal and integer places, and optional prefixes and suffixes · Can produce currency and percentage formats and can be extended to produce other formats ...
🌐
Coderanch
coderanch.com › t › 393717 › java › Number-Format-Pattern-Syntax
Number Format Pattern Syntax (Beginning Java forum at Coderanch)
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums · this forum made possible by our volunteer staff, including ... ... Can anyone explain how to read: '\\u0000'..'\\uFFFD' - specialCharacters It is part of Number Format Pattern Syntax: pattern := subpattern{;subpattern} subpattern := {prefix}integer{.fraction}{suffix} prefix := '\\u0000'..'\\uFFFD' - specialCharacters suffix := '\\u0000'..'\\uFFFD' - specialCharacters integer := '#'* '0'* '0' fraction := '0'* '#'* found at http://java.sun.com/docs/books/tutorial/java/data/numberpattern.html Thanks!
🌐
Tutorial Gateway
tutorialgateway.org › java-number-pattern-programs
Java Number Pattern Programs
August 25, 2026 - Simple Number Pattern Rows = 9 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 1 2 3 4 5 6 1 2 3 4 5 6 7 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 9 · For more information on printing the Java Floyd’s Triangle program.
🌐
Stack Overflow
stackoverflow.com › questions › 61210152 › format-number-in-java-which-is-the-correct-pattern
spring - Format number in java, which is the correct pattern - Stack Overflow
NumberFormat nf = NumberFormat... = df.format(myNum); System.out.println(output); In the format pattern you cannot change group separator , and decimal separator .....
🌐
Blogger
javarevisited.blogspot.com › 2012 › 03 › how-to-format-decimal-number-in-java.html
How to format Decimal Number in Java? DecimalFormat Example
April 12, 2025 - Thankfully Java programming language provides many different ways to format numbers in Java like either using Math.round() or setScale() from BigDecimal but the caveat is that they also do rounding of numbers i.e. 1.6 will be rounded on 2.0 ...
🌐
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...