Class yang perlu diimport :
1 | import java.text.DecimalFormat; |
2 | import java.text.DecimalFormatSymbols; |
3 | import java.text.NumberFormat; |
4 | import java.util.Locale; |
01 | void formatNumber(){ |
02 | double harga = 6500000 ; |
03 | // jenis pertama |
04 | String strFormat0 = "####" ; |
05 | DecimalFormat df = new DecimalFormat(strFormat0); |
06 | System.out.println(df.format(harga)); |
07 | //output 6500000 |
08 |
09 | // jenis pertama |
10 | String strFormat1 = "####.00" ; |
11 | DecimalFormat df2 = new DecimalFormat(strFormat1); |
12 | System.out.println(df2.format(harga)); |
13 | //output 6500000.00 |
14 |
15 | // jenis pertama |
16 | String strFormat2 = "#,###.00" ; |
17 | DecimalFormat df3 = new DecimalFormat(strFormat2); |
18 | System.out.println(df3.format(harga)); |
19 | //output 6,500,000.00 |
20 |
21 | // jenis pertama |
22 | String strFormat3 = "#,###.00" ; |
23 | DecimalFormat df4 = new DecimalFormat(strFormat3, |
24 | new DecimalFormatSymbols(Locale.GERMAN)); |
25 | System.out.println(df4.format(harga)); |
26 | //output 6.500.000,00 |
27 | } |
Tidak ada komentar:
Posting Komentar