- JAVA Internalization Tutorial
- JAVA I18N - Home
- JAVA I18N - Overview
- JAVA I18N - Environment Setup
- Locale Class Examples
- JAVA I18N - Locale Class
- JAVA I18N - Locale Details
- JAVA I18N - Display Language
- ResourceBundle Class Examples
- JAVA I18N - ResourceBundle Class
- NumberFormat Class Examples
- JAVA I18N - NumberFormat Class
- JAVA I18N - Format Currencies
- JAVA I18N - Format Percentages
- JAVA I18N - Set Min/Max Precision
- JAVA I18N - Set Rounding Mode
- JAVA I18N - Parsing Numbers
- DecimalFormat Class Examples
- JAVA I18N - DecimalFormat Class
- JAVA I18N - Formatting Patterns
- JAVA I18N - Locale Specific DecimalFormat
- JAVA I18N - DecimalFormatSymbols Class
- JAVA I18N - Grouping Digits
- DateFormat Class Examples
- JAVA I18N - DateFormat Class
- JAVA I18N - Formatting Dates
- JAVA I18N - Formatting Time
- JAVA I18N - Formatting Date and Time
- SimpleDateFormat Class Examples
- JAVA I18N - SimpleDateFormat Class
- JAVA I18N - Formatting Date
- JAVA I18N - DateFormatSymbols Class
- JAVA I18N - Date Format Patterns
- Time Zones Examples
- JAVA I18N - UTC
- Unicode Conversion
- JAVA I18N - From and To String Conversion
- JAVA I18N - From Reader and To Writer Conversion
- JAVA Internalization Useful Resources
- Java I18N - Quick Guide
- Java I18N - Useful Resources
- Java I18N - Discussion
Java Internalization - Locale Specific DecimalFormat
By default, DecimalFormat object is using the JVM's locale. We can change the default locale while creating the DecimalFormat object using NumberFormat class. In the example below, we'll use same pattern for two different locale and you can spot the difference in the output.
IOTester.java
import java.text.DecimalFormat; import java.text.NumberFormat; import java.util.Locale; public class I18NTester { public static void main(String[] args) { String pattern = "###.##"; double number = 123.45; Locale enlocale = new Locale("en", "US"); Locale dalocale = new Locale("da", "DK"); DecimalFormat decimalFormat = (DecimalFormat) NumberFormat.getNumberInstance(enlocale); decimalFormat.applyPattern(pattern); System.out.println(decimalFormat.format(number)); decimalFormat = (DecimalFormat) NumberFormat.getNumberInstance(dalocale); decimalFormat.applyPattern(pattern); System.out.println(decimalFormat.format(number)); } }
Output
It will print the following result.
123.45 123,45Print
Advertisements