DecimalFormat Java Example: Master Number Formatting Easily

Robert Jul 13, 2026

The DecimalFormat class in Java is a powerful tool for formatting decimal numbers as strings. It's part of the java.text package and provides a wide range of formatting options, including specifying decimal separator, grouping size, and precision.

How to format Decimal Number in Java? DecimalFormat Example
How to format Decimal Number in Java? DecimalFormat Example

Whether you're dealing with currency, percentages, or scientific notation, DecimalFormat enables you to present numerical data in a human-readable and culturally appropriate manner. Let's dive into some examples to illustrate its capabilities.

Java Cheatsheet
Java Cheatsheet

Basic Formatting

At its core, DecimalFormat allows you to format a decimal number with specified precision and grouping size.

a poster with different types of writing and numbers on it, including the words'jwa beginner notes '
a poster with different types of writing and numbers on it, including the words'jwa beginner notes '

For instance, to format a double as a currency with two decimal places and a thousand separator:

```java import java.text.DecimalFormat; DecimalFormat df = new DecimalFormat("#,##0.00"); double number = 123456.789; String formattedNumber = df.format(number); ```

Pattern Syntax

Java Patterns Notes 1
Java Patterns Notes 1

The pattern syntax used by DecimalFormat is quite intuitive. Here are some examples:

  • #,##0 - Thousands separator, no decimals (e.g., 1,234,567)
  • #,##0.00 - Thousands separator, two decimals (e.g., 1,234,567.89)
  • 0.00; - Custom decimal separator (semicolon in this case)
  • ######.## - No grouping, fixed precision

Locale-Aware Formatting

Java Data Types Explained
Java Data Types Explained

By using DecimalFormat with the current locale, you can ensure that the output respects the user's locale settings.

Here's how to create a locale-aware DecimalFormat:

```java import java.text.NumberFormat; import java.util.Locale; NumberFormat nf = NumberFormat.getNumberInstance(Locale.getDefault()); DecimalFormat df = (DecimalFormat) nf; ```

Advanced Formatting

Free Java Programming Book
Free Java Programming Book

Sometimes, you might need more control over the formatting. This is where DecimalFormatSymbols comes in handy.

Customizing Separators and Prefixes/Suffixes

Data Types in Java
Data Types in Java
β˜• Java Basics Cheat Sheet for Beginners | Learn Java in One Page πŸš€
β˜• Java Basics Cheat Sheet for Beginners | Learn Java in One Page πŸš€
πŸ‘‰ Variables πŸ‘ˆ
πŸ‘‰ Variables πŸ‘ˆ
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 Collection Framework: ArrayList Explained Visually
Java Collection Framework: ArrayList Explained Visually
Java Data Types Made Easy for Beginners
Java Data Types Made Easy for Beginners
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
30 Days of Java – Day 2 | Variables & Data Types Explained for Beginners and students
30 Days of Java – Day 2 | Variables & Data Types Explained for Beginners and students
Learn Java Collections Fast πŸš€ | ArrayList HashMap Set Queue Explained
Learn Java Collections Fast πŸš€ | ArrayList HashMap Set Queue Explained
How to find size, Default, Maximum and Minimum values of Primitive Data Types in Java? Example Tutorial
How to find size, Default, Maximum and Minimum values of Primitive Data Types in Java? Example Tutorial
Java Type Promotion, Type Casting & Conversion Notes
Java Type Promotion, Type Casting & Conversion Notes
Master Two Pointers Technique πŸ’» | Java DSA Interview Preparation Guide
Master Two Pointers Technique πŸ’» | Java DSA Interview Preparation Guide
Java Data Types Cheat Sheet πŸ’»πŸ”₯ | Beginner Friendly
Java Data Types Cheat Sheet πŸ’»πŸ”₯ | Beginner Friendly
an image of a computer screen with text
an image of a computer screen with text
If you’re learning Java backend in 2026, this is the exact order I’d recommend β€” from strong foundations to industry-ready Spring skills. πŸ“˜ Image 1 β†’ Core Java, JVM, DSA, Web & REST basics πŸ“— Image… | Aditya Mishra
If you’re learning Java backend in 2026, this is the exact order I’d recommend β€” from strong foundations to industry-ready Spring skills. πŸ“˜ Image 1 β†’ Core Java, JVM, DSA, Web & REST basics πŸ“— Image… | Aditya Mishra
Java Conditional Statements Cheat Sheet
Java Conditional Statements Cheat Sheet
Default values in java
Default values in java
Java Class and Objects - Easy Learning with Real-life Examples! - TechVidvan
Java Class and Objects - Easy Learning with Real-life Examples! - TechVidvan
Level Up Your Java πŸš€ Intermediate Cheat Sheet You’ll Actually Use
Level Up Your Java πŸš€ Intermediate Cheat Sheet You’ll Actually Use

By creating a DecimalFormatSymbols instance and setting its properties, you can customize your format:

```java import java.text.DecimalFormatSymbols; DecimalFormatSymbols dfs = new DecimalFormatSymbols(); dfs.setDecimalSeparator('.'); dfs.setGroupingSeparator(','); dfs.setCurrencySymbol('$'); dfs.setMonetaryDecimalSeparator('.'); DecimalFormat df = new DecimalFormat("Β€,#00.00", dfs); ```

Scientific Notation

Formatting large or small numbers in scientific notation can be achieved using DecimalFormat with an appropriate pattern:

```java DecimalFormat df = new DecimalFormat("0.###########E0"); double number = 1234567890.12345; String formattedNumber = df.format(number); ```

The final result might vary depending on the locale settings, but using 0.###########E0 will show the number in scientific notation, with 9 significant figures and without a grouping separator.

In the vast landscape of Java's text formatting utilities, DecimalFormat stands out as an invaluable tool for converting numbers into human-readable strings. Its flexibility and comprehensiveness make it an essential part of any developer's toolkit. Happy formatting!