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 OverflowOracle
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.
06:52
Java tutorial #44 | Some useful classes - Number Formatting | Java ...
Formatting Money & Percents in Java using the NumberFormat ...
01:51
Java Currency Number Format | How to Currency Format in Java | ...
03:32
103. Formatting numbers as currency values - Learn Java - YouTube
04:28
Java DecimalFormat - Java Number Format - YouTube
Top answer 1 of 9
125
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.
2 of 9
74
You and String.format() will be new best friends!
https://docs.oracle.com/javase/1.5.0/docs/api/java/util/Formatter.html#syntax
String.format("%.2f", (double)value);
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.
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...
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
Top answer 1 of 4
3
Use DecimalFormat()
DecimalFormat df2 = new DecimalFormat( "#,###,###,##0.00" );
double d = 100.2397;
double d1 = new Double(df2.format(d)).doubleValue();
Output - d1 will be 100.24
2 of 4
3
You need to use a number formatter as shown in the example below:
NumberFormat myFormatter = new DecimalFormat("#.###");
double myNumber = 1234.5632;
String formattedNumber = myFormatter.format(myNumber);
and formattedNumber will be equal to "1234.563"
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.