- //獲取DecimalFormat的方法DecimalFormat.getInstance();
-
- public static void test1(DecimalFormat df) {
- //默認顯示3位小數
- double d = 1.5555555;
- System.out.println(df.format(d));//1.556
- //設置小數點后最大位數為5
- df.setMaximumFractionDigits(5);
- df.setMinimumIntegerDigits(15);
- System.out.println(df.format(d));//1.55556
- df.setMaximumFractionDigits(2);
- System.out.println(df.format(d));//1.56
- //設置小數點后最小位數,不夠的時候補0
- df.setMinimumFractionDigits(10);
- System.out.println(df.format(d));//1.5555555500
- //設置整數部分最小長度為3,不夠的時候補0
- df.setMinimumIntegerDigits(3);
- System.out.println(df.format(d));
- //設置整數部分的最大值為2,當超過的時候會從個位數開始取相應的位數
- df.setMaximumIntegerDigits(2);
- System.out.println(df.format(d));
- }
-
- public static void test2(DecimalFormat df) {
- int number = 155566;
- //默認整數部分三個一組,
- System.out.println(number);//輸出格式155,566
- //設置每四個一組
- df.setGroupingSize(4);
- System.out.println(df.format(number));//輸出格式為15,5566
- DecimalFormatSymbols dfs = DecimalFormatSymbols.getInstance();
- //設置小數點分隔符
- dfs.setDecimalSeparator(';');
- //設置分組分隔符
- dfs.setGroupingSeparator('a');
- df.setDecimalFormatSymbols(dfs);
- System.out.println(df.format(number));//15a5566
- System.out.println(df.format(11.22));//11;22
- //取消分組
- df.setGroupingUsed(false);
- System.out.println(df.format(number));
- }
-
- public static void test3(DecimalFormat df) {
- double a = 1.220;
- double b = 11.22;
- double c = 0.22;
- //占位符可以使用0和#兩種,當使用0的時候會嚴格按照樣式來進行匹配,不夠的時候會補0,而使用#時會將前后的0進行忽略
- //按百分比進行輸出
- // df.applyPattern("00.00%");
- df.applyPattern("##.##%");
- System.out.println(df.format(a));//122%
- System.out.println(df.format(b));//1122%
- System.out.println(df.format(c));//22%
- double d = 1.22222222;
- //按固定格式進行輸出
- df.applyPattern("00.000");
- System.out.println(df.format(d));//01.222
- df.applyPattern("##.###");
- System.out.println(df.format(d));//1.222
- }
(自己的實例)// public class Test1 {
public static void main(String[] args) { // TODO Auto-generated method stub File file = new File("E:\\KuGou\\陳惠英 - 今世有緣.mp3"); StringBuilder str = new StringBuilder(); Calendar ca = Calendar.getInstance(); ca.setTimeInMillis(file.lastModified()); str.append("最后修改日期"+ca.getTime().toLocaleString()); str.append("\n");
//數字分節(jié) DecimalFormat df3 = new DecimalFormat("000,000"); str.append("文件大小"+df3.format(file.length())); str.append("\n"); if(file.isDirectory()) { str.append("類型: 目錄"); } else { str.append("類型: 文件"); } System.out.println(str.toString()); }
}
|