簡單使用DecimalFormat的功能就能做到了,代碼如下:
package com.testEmp; import java.text.DecimalFormat; public class NumberFormat { public static void main(String[] args) { long[] arr= {1,2345,676767,664564545,4324324324432432443L}; for(long num:arr) { System.out.println(toEastNumFormat(num)); } } // 將整數在千分位以逗號分隔表示 public static String toWestNumFormat(long number) { DecimalFormat df = new DecimalFormat("#,###"); return df.format(number); } // 將整數在萬分位以逗號分隔表示 public static String toEastNumFormat(long number) { DecimalFormat df = new DecimalFormat("#,####"); return df.format(number); } }
運行示例:
1 2345 67,6767 6,6456,4545 432,4324,3244,3243,2443
1 2,345 676,767 664,564,545 4,324,324,324,432,432,443
--END 2019年10月5日07:34:06--