Locale format
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.NumberFormat;
import java.util.Locale;
public class Main {
static public void localizedFormat(String pattern, double value, Locale loc) {
NumberFormat nf = NumberFormat.getNumberInstance(loc);
DecimalFormat df = (DecimalFormat) nf;
df.applyPattern(pattern);
String output = df.format(value);
System.out.println(pattern + " " + output + " " + loc.toString());
}
static public void main(String[] args) {
Locale currentLocale = new Locale("en", "US");
DecimalFormatSymbols unusualSymbols = new DecimalFormatSymbols(
currentLocale);
unusualSymbols.setDecimalSeparator('|');
unusualSymbols.setGroupingSeparator('^');
String strange = "#,##0.###";
DecimalFormat weirdFormatter = new DecimalFormat(strange, unusualSymbols);
weirdFormatter.setGroupingSize(4);
String bizarre = weirdFormatter.format(12345.678);
System.out.println(bizarre);
Locale[] locales = { new Locale("en", "US"), new Locale("de", "DE"),
new Locale("fr", "FR") };
for (int i = 0; i < locales.length; i++) {
localizedFormat("###,###.###", 123456.789, locales[i]);
}
}
}
Output:
1^2345|678
###,###.### 123,456.789 en_US
###,###.### 123.456,789 de_DE
###,###.### 123456,789 fr_FR
Home
Java Book
Runnable examples
Java Book
Runnable examples
Data Type Format:
- Decimal Format with different Symbols
- 0 symbol shows a digit or 0 if no digit present
- Currency value
- Decimal separator is set to "|"
- Fraction Digits Minimum
- Fraction Digits Maximum
- Group separators and show trailing zeroes
- Leading zeroes
- Percentage
- Round number to fewer decimals
- Scientific notation: 0.######E0 and 0.0E0
- . symbol indicates the decimal point
- ; symbol specifies an alternate pattern for negative values
- ' symbol quotes literal symbols
- The number of # to the left of the decimal point sets the multiple of the exponent.
- Locale format
- Locale US default format
- Number Format:0.00
- Number Format:#.# (one decimal point)
- Number Format:abc#
- Number Format:00E00 and 000E00
- Number Format:0000000000E0
- Number Format:#.###### (more than one decimal point)
- Number Format:#.000000
- Number Format:.###### (six decimal points)
- Number Format:#,###,### (group)
- Number Format:##00
- Number Format:00.00E0
- Number Format:0.######E0
- Number Format:000000E0
- Parse a string to a number using a NumberFormat