# Formatting a number in a locale-sensitive manner

Use the class DecimalFormat. Set the pattern, a String that specifies how a number should be formatted, with applyPattern and invoke the method format which will return the formatted number.

In creating a pattern, you can use the following symbols: (from API)

```0		digit
#		digit, zero shown as absent
.		placeholder for decimal separator
,		placeholder for grouping separator
E		separates mantissa and exponent for exponential formats
;		separates formats
-		default negative prefix
%		multiply by 100 and show as percentag
u2030	multiply by 1000 and show as per mille
u00a4	currency sign; replaced by currency symbol; if doubled, replaced by
international currency symbol.  If present in a pattern, the monetary
decimal separator is used instead of the decimal separator
X		any other characters can be used in the prefix or suffix
'		used to quote special characters in a prefix or suffix
```

If you specify an pattern that makes no sense, a ParseException will be thrown.

Main.java:

```import java.text.*;
import java.util.*;

public class Main {
static DecimalFormat decimalFormat = (DecimalFormat)NumberFormat.getInstance();

public static void main(String args[]) {

double numbers[] = { 0.0, 0.123, -0.123, 1.0, -1.0, 20.5, -20.5, 123, -123, 234567 };
String patterns[] = { "##0.##", "###,###.##", "0,000.0", "0", "####-", "\$000.##",
"BEF ###", "#%", "u00a4u00a4 ###", "#;(NEG!) #-" };

for (int i=0; i<patterns.length; i++) {
for (int j=0; j<numbers.length; j++) {
formatDouble(patterns[i], numbers[j]);
}
System.out.println();
}
}

public static void formatDouble(String pattern, double number) {
decimalFormat.applyPattern(pattern);
String result = decimalFormat.format(number);

columnize(""+pattern, 15);
columnize(""+number, 15);
columnize(result, 15);

System.out.println();
}

public static void columnize(String s, int col) {
for (int i=0; i<col-s.length(); i++) {
System.out.print(' ');
}
System.out.print(s);
}
}```

outputs:

```         ##0.##            0.0              0
##0.##          0.123           0.12
##0.##         -0.123          -0.12
##0.##            1.0              1
##0.##           -1.0             -1
##0.##           20.5           20.5
##0.##          -20.5          -20.5
##0.##          123.0            123
##0.##         -123.0           -123
##0.##       234567.0         234567

###,###.##            0.0              0
###,###.##          0.123           0.12
###,###.##         -0.123          -0.12
###,###.##            1.0              1
###,###.##           -1.0             -1
###,###.##           20.5           20.5
###,###.##          -20.5          -20.5
###,###.##          123.0            123
###,###.##         -123.0           -123
###,###.##       234567.0        234,567

0,000.0            0.0        0,000.0
0,000.0          0.123        0,000.1
0,000.0         -0.123       -0,000.1
0,000.0            1.0        0,001.0
0,000.0           -1.0       -0,001.0
0,000.0           20.5        0,020.5
0,000.0          -20.5       -0,020.5
0,000.0          123.0        0,123.0
0,000.0         -123.0       -0,123.0
0,000.0       234567.0      234,567.0

0            0.0              0
0          0.123              0
0         -0.123             -0
0            1.0              1
0           -1.0             -1
0           20.5             20
0          -20.5            -20
0          123.0            123
0         -123.0           -123
0       234567.0         234567

####-            0.0             0-
####-          0.123             0-
####-         -0.123            -0-
####-            1.0             1-
####-           -1.0            -1-
####-           20.5            20-
####-          -20.5           -20-
####-          123.0           123-
####-         -123.0          -123-
####-       234567.0        234567-

\$000.##            0.0           \$000
\$000.##          0.123        \$000.12
\$000.##         -0.123       -\$000.12
\$000.##            1.0           \$001
\$000.##           -1.0          -\$001
\$000.##           20.5         \$020.5
\$000.##          -20.5        -\$020.5
\$000.##          123.0           \$123
\$000.##         -123.0          -\$123
\$000.##       234567.0        \$234567

BE###            0.0            BE0
BE###          0.123            BE0
BE###         -0.123           -BE0
BE###            1.0            BE1
BE###           -1.0           -BE1
BE###           20.5           BE20
BE###          -20.5          -BE20
BE###          123.0          BE123
BE###         -123.0         -BE123
BE###       234567.0       BE234567

#%            0.0             0%
#%          0.123            12%
#%         -0.123           -12%
#%            1.0           100%
#%           -1.0          -100%
#%           20.5          2050%
#%          -20.5         -2050%
#%          123.0         12300%
#%         -123.0        -12300%
#%       234567.0      23456700%

###            0.0          USD 0
###          0.123          USD 0
###         -0.123         -USD 0
###            1.0          USD 1
###           -1.0         -USD 1
###           20.5         USD 20
###          -20.5        -USD 20
###          123.0        USD 123
###         -123.0       -USD 123
###       234567.0     USD 234567

#;(NEG!) #-            0.0              0
#;(NEG!) #-          0.123              0
#;(NEG!) #-         -0.123      (NEG!) 0-
#;(NEG!) #-            1.0              1
#;(NEG!) #-           -1.0      (NEG!) 1-
#;(NEG!) #-           20.5             20
#;(NEG!) #-          -20.5     (NEG!) 20-
#;(NEG!) #-          123.0            123
#;(NEG!) #-         -123.0    (NEG!) 123-
#;(NEG!) #-       234567.0         234567
```