NumberFormat
About
The NumberFormat class in Java is part of the java.text package and provides a framework for formatting and parsing numbers according to different locales. It is commonly used for formatting numbers, currencies, and percentages in a locale-sensitive manner.
NumberFormatis an abstract class that formats and parses numbers for any locale.It simplifies handling of number-related formatting tasks like currency representation, percentage formatting, or custom patterns.
The class supports both formatting (conversion of numbers to strings) and parsing (conversion of strings to numbers).
Features
Locale-Sensitive Formatting: Adapts number formats to the conventions of a specific locale, such as decimal separators, grouping separators, and currency symbols.
Predefined Number Styles: Includes predefined methods to create number formatters for default numbers, currencies, and percentages.
Customizability: Can be customized using sub-classes like
DecimalFormatfor more advanced formatting.Parsing Support: Converts formatted strings back into numbers, ensuring consistency in applications where numbers need to be read back from user input or data files.
Thread-Safe Instances: Instances of
NumberFormatcan be used in multi-threaded applications.
Declaration
To use the NumberFormat class, we must import it from the java.text package:
import java.text.NumberFormat;
import java.util.Locale;Common Methods
Static Factory Methods:
getInstance(): Returns a general-purpose number formatter for the default locale.getCurrencyInstance(): Returns a currency formatter for the default locale.getPercentInstance(): Returns a percentage formatter for the default locale.getNumberInstance(): Returns a formatter for numbers.getInstance(Locale locale): Returns a formatter for a specific locale.
Formatting Methods:
format(double number): Formats adoubleas aString.format(long number): Formats alongas aString.
Parsing Methods:
parse(String source): Parses aStringinto aNumber.
Customization:
setMaximumFractionDigits(int newValue): Sets the maximum number of fraction digits.setMinimumFractionDigits(int newValue): Sets the minimum number of fraction digits.setGroupingUsed(boolean newValue): Enables or disables grouping separators (e.g., commas).
Other Utility Methods:
setCurrency(Currency currency): Sets the currency for currency formatting.getCurrency(): Retrieves the currency used for formatting.
Usage
Formatting Numbers
Formatting Currency for Different Locales
Output:
Formatting Percentages
Output:
Parsing Numbers
Output:
Applications and Real-World Usage
Currency Display in E-Commerce: Display prices in local currencies based on the user’s locale.
Percentage Calculations: Format percentages in analytics dashboards or reports.
Locale-Sensitive Input and Output: Format and parse numbers in applications that support multiple regions or languages.
Finance Applications: Display monetary values with appropriate separators, currency symbols, and decimal precision.
Data Visualization: Format numerical data for graphs, charts, and tables.
Last updated