Custom 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
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
1234567891011121314151617181920package 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 ______.
Tak for dine kommentarer!
Spørg AI
Spørg AI
Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat
Awesome!
Completion rate improved to 5.56
Custom Patterns and Symbols
Stryg for at vise menuen
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
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
1234567891011121314151617181920package 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 ______.
Tak for dine kommentarer!