DecimalFormat Class Java Example Tutorial - Format Numbers Easily

Robert Jul 13, 2026

The DecimalFormat class in Java is a fundamental tool for formatting decimal numbers. It allows for precise control over the appearance of numbers, dates, and strings in Java programming. The DecimalFormat class is part of the java.text package and extends the NumberFormat abstract class.

Java Class and Objects - Easy Learning with Real-life Examples! - TechVidvan
Java Class and Objects - Easy Learning with Real-life Examples! - TechVidvan

In this article, we'll explore the DecimalFormat class with examples, delve into its key features, and discuss its practical applications. By the end, you'll have a solid understanding of how to use this class to format decimal numbers in Java.

Class and Object in Java Explained | OOP Concepts for Beginners
Class and Object in Java Explained | OOP Concepts for Beginners

DecimalFormat Syntax and Basic Usage

The DecimalFormat class uses a pattern to format numbers. These patterns follow a specific syntax. Understanding the syntax is key to working effectively with DecimalFormat.

Java Cheatsheet
Java Cheatsheet

The basic syntax is as follows:

  • #,###.###: This pattern will output numbers with thousands separators and decimal separators.
  • 000.00: This pattern will pad the output with leading zeros if necessary. It will also output a decimal place even if the number is not fractional.
Java Programming CheatsheetIntro to Java Programming: An Interdisciplinary Approach and Computer Science: An Interdisciplinary Approach by Sedgewick and Wayne
Java Programming CheatsheetIntro to Java Programming: An Interdisciplinary Approach and Computer Science: An Interdisciplinary Approach by Sedgewick and Wayne

Number Patterns

The # symbol represents any digit (0-9), while '0' represents a digit that must be present. The ',' symbol separates groups of three digits. The '.' symbol separates the integer part from the fractional part.

Setting the Pattern

Decimal of the Day
Decimal of the Day

To set the pattern in DecimalFormat, you can use either the constructor or the applyPattern method.

Here's an example of setting the pattern via the constructor:

```java DecimalFormat df = new DecimalFormat("#,###.###"); ```

Formatting Numbers with DecimalFormat

Java Patterns Notes 1
Java Patterns Notes 1

Once you've set the pattern, you can use the format method to format numbers. This method takes a Number object as an argument and returns a String.

Here's an example:

three types of variable type in an organization
three types of variable type in an organization
Free Java Programming Book
Free Java Programming Book
fun2dolabs - Etsy India
fun2dolabs - Etsy India
What is number class in Java?
What is number class in Java?
Java Type Promotion, Type Casting & Conversion Notes
Java Type Promotion, Type Casting & Conversion Notes
👉 Variables 👈
👉 Variables 👈
Rules Of Decimals
Rules Of Decimals
30 Days of Java – Day 10 | Java Method Parameters Explained for Beginners
30 Days of Java – Day 10 | Java Method Parameters Explained for Beginners
Java Data Types Explained
Java Data Types Explained
the fraction addition worksheet is shown in black and white with numbers, symbols, and
the fraction addition worksheet is shown in black and white with numbers, symbols, and
Default values in java
Default values in java
☕ Java Basics Cheat Sheet for Beginners | Learn Java in One Page 🚀
☕ Java Basics Cheat Sheet for Beginners | Learn Java in One Page 🚀
Data Types in Java
Data Types in Java
How to Declare Classes in Java? | Java Declaring Classes | Java Tutorial
How to Declare Classes in Java? | Java Declaring Classes | Java Tutorial
Java Data Types Made Easy for Beginners
Java Data Types Made Easy for Beginners
an image of a computer screen with many lines on the screen and numbers in different colors
an image of a computer screen with many lines on the screen and numbers in different colors
Java Data Type, Primitive and Non-Primitive Data type, Java Variables
Java Data Type, Primitive and Non-Primitive Data type, Java Variables
a pink and orange poster with words on it that say,'what is the meaning?
a pink and orange poster with words on it that say,'what is the meaning?
Java Math.ceil() Method with Examples
Java Math.ceil() Method with Examples
Learn Java Collections Fast 🚀 | ArrayList HashMap Set Queue Explained
Learn Java Collections Fast 🚀 | ArrayList HashMap Set Queue Explained

```java Number num = 123456.789; String formattedNum = df.format(num); System.out.println(formattedNum); // Outputs: 123,456.789 ```

Formatting and Parsing

The DecimalFormat class also provides the parse method for parsing strings back into numbers. This is useful when working with string inputs and you need the numeric value.

DecimalFormatSymbols

The DecimalFormatSymbols class allows you to change the default formatting symbols (like the decimal separator, grouping separator, and the negative sign). This is helpful when working with localized formatting.

Here's an example of changing the decimal separator to a comma:

```java DecimalFormatSymbols dfs = new DecimalFormatSymbols(); dfs.setDecimalSeparator('.'); DecimalFormat df = new DecimalFormat("###,###.###", dfs); ```

With this example, the output of df.format(123456.789) will be '123.456,789'.

Using DecimalFormat in Practical Scenarios

Understanding how to use DecimalFormat is crucial in many programming scenarios. From displaying financial data to scientific calculations, DecimalFormat plays a pivotal role in Java's formatting capabilities.

Financial Applications

In financial applications, DecimalFormat can be used to format currency amounts, percentages, and other monetary values. Here's an example of formatting a currency amount:

```java Number price = 123456.789; DecimalFormat currencyFormat = new DecimalFormat("$ ###,###.##"); String formattedPrice = currencyFormat.format(price); System.out.println(formattedPrice); // Outputs: $123,456.79 ```

Scientific Calculations

When working with scientific data, DecimalFormat can be used to display numbers with a specific number of decimal places. Here's an example of formatting a number to two decimal places:

```java Number num = 123456.789; DecimalFormat df = new DecimalFormat("0.##"); String formattedNum = df.format(num); System.out.println(formattedNum); // Outputs: 123456.79 ```

Formatting numbers in Java can be a powerful tool in your programming toolkit. The DecimalFormat class, with its extensive formatting capabilities, is a robust solution for many common formatting tasks. Understanding its syntax, methods, and practical applications will greatly enhance your productivity as a Java developer.

To continue practicing and exploring the DecimalFormat class, consider challenging yourself with more complex formatting tasks. You might find it helpful to explore the JavaDoc for the DecimalFormat class and its related classes for more detailed information. Happy coding!