Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprende Custom Patterns and Symbols | Controlling Numeric Output with DecimalFormat
Quizzes & Challenges
Quizzes
Challenges
/
Formatting & Parsing in Java

bookCustom Patterns and Symbols

When you need precise control over how numbers are displayed in Java, the DecimalFormat class offers a rich set of pattern symbols. These symbols allow you to format numbers as currency, percentages, or even in scientific notation. Understanding the core pattern characters is essential for building custom formats that match your application's requirements.

The most common pattern symbols in DecimalFormat include:

  • #: Digit, shows only if needed;
  • 0: Digit, always shows (pads with zeros if necessary);
  • .: Decimal separator;
  • ,: Grouping separator (such as thousands separator);
  • %: Multiplies the number by 100 and displays as a percentage;
  • ¤: Currency sign, replaced by the currency symbol for the locale;
  • E: Separates mantissa and exponent in scientific notation.

By combining these symbols, you can create custom patterns for a wide range of numeric outputs.

Main.java

Main.java

copy
12345678910111213141516171819202122232425
// File: CustomPatternDemo.java package com.example; import java.text.DecimalFormat; public class Main { public static void main(String[] args) { double amount = 12345.678; double rate = 0.075; double scientific = 9876543.21; // Currency formatting DecimalFormat currencyFormat = new DecimalFormat("¤#,##0.00"); System.out.println("Currency: " + currencyFormat.format(amount)); // Percentage formatting DecimalFormat percentFormat = new DecimalFormat("0.0%"); System.out.println("Percentage: " + percentFormat.format(rate)); // Scientific notation DecimalFormat sciFormat = new DecimalFormat("0.###E0"); System.out.println("Scientific: " + sciFormat.format(scientific)); } }

You can go even further by customizing the symbols used in your formatted numbers. The DecimalFormatSymbols class lets you change the decimal separator, grouping separator, currency symbol, and more. This is especially useful when you need to support different conventions or create a specific output style.

To use DecimalFormatSymbols, create an instance, set your desired symbols, and then pass it to the DecimalFormat constructor alongside your pattern.

Main.java

Main.java

copy
1234567891011121314151617181920
package com.example; import java.text.DecimalFormat; import java.text.DecimalFormatSymbols; public class Main { public static void main(String[] args) { double value = 1234567.89; // Create custom symbols DecimalFormatSymbols symbols = new DecimalFormatSymbols(); symbols.setDecimalSeparator(','); symbols.setGroupingSeparator(' '); // Apply custom symbols to format DecimalFormat customFormat = new DecimalFormat("#,##0.00", symbols); System.out.println("Custom separators: " + customFormat.format(value)); } }

1. What does the '#' symbol represent in a DecimalFormat pattern?

2. How can you format a number as a percentage using DecimalFormat?

3. Fill in the blank: To format a number in scientific notation, use the pattern ______.

question mark

What does the '#' symbol represent in a DecimalFormat pattern?

Select the correct answer

question mark

How can you format a number as a percentage using DecimalFormat?

Select the correct answer

question-icon

Fill in the blank: To format a number in scientific notation, use the pattern ______.

9.877E6

Click or drag`n`drop items and fill in the blanks

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 2. Capítulo 2

Pregunte a AI

expand

Pregunte a AI

ChatGPT

Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla

bookCustom Patterns and Symbols

Desliza para mostrar el menú

When you need precise control over how numbers are displayed in Java, the DecimalFormat class offers a rich set of pattern symbols. These symbols allow you to format numbers as currency, percentages, or even in scientific notation. Understanding the core pattern characters is essential for building custom formats that match your application's requirements.

The most common pattern symbols in DecimalFormat include:

  • #: Digit, shows only if needed;
  • 0: Digit, always shows (pads with zeros if necessary);
  • .: Decimal separator;
  • ,: Grouping separator (such as thousands separator);
  • %: Multiplies the number by 100 and displays as a percentage;
  • ¤: Currency sign, replaced by the currency symbol for the locale;
  • E: Separates mantissa and exponent in scientific notation.

By combining these symbols, you can create custom patterns for a wide range of numeric outputs.

Main.java

Main.java

copy
12345678910111213141516171819202122232425
// File: CustomPatternDemo.java package com.example; import java.text.DecimalFormat; public class Main { public static void main(String[] args) { double amount = 12345.678; double rate = 0.075; double scientific = 9876543.21; // Currency formatting DecimalFormat currencyFormat = new DecimalFormat("¤#,##0.00"); System.out.println("Currency: " + currencyFormat.format(amount)); // Percentage formatting DecimalFormat percentFormat = new DecimalFormat("0.0%"); System.out.println("Percentage: " + percentFormat.format(rate)); // Scientific notation DecimalFormat sciFormat = new DecimalFormat("0.###E0"); System.out.println("Scientific: " + sciFormat.format(scientific)); } }

You can go even further by customizing the symbols used in your formatted numbers. The DecimalFormatSymbols class lets you change the decimal separator, grouping separator, currency symbol, and more. This is especially useful when you need to support different conventions or create a specific output style.

To use DecimalFormatSymbols, create an instance, set your desired symbols, and then pass it to the DecimalFormat constructor alongside your pattern.

Main.java

Main.java

copy
1234567891011121314151617181920
package com.example; import java.text.DecimalFormat; import java.text.DecimalFormatSymbols; public class Main { public static void main(String[] args) { double value = 1234567.89; // Create custom symbols DecimalFormatSymbols symbols = new DecimalFormatSymbols(); symbols.setDecimalSeparator(','); symbols.setGroupingSeparator(' '); // Apply custom symbols to format DecimalFormat customFormat = new DecimalFormat("#,##0.00", symbols); System.out.println("Custom separators: " + customFormat.format(value)); } }

1. What does the '#' symbol represent in a DecimalFormat pattern?

2. How can you format a number as a percentage using DecimalFormat?

3. Fill in the blank: To format a number in scientific notation, use the pattern ______.

question mark

What does the '#' symbol represent in a DecimalFormat pattern?

Select the correct answer

question mark

How can you format a number as a percentage using DecimalFormat?

Select the correct answer

question-icon

Fill in the blank: To format a number in scientific notation, use the pattern ______.

9.877E6

Click or drag`n`drop items and fill in the blanks

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 2. Capítulo 2
some-alt