java数字千分位格式转换
public static void main(String[] args) {
System.out.println(thousandsSeparator("123123131"));
}
public static String thousandsSeparator(String value) {
if (isNotNull(value)) {
String[] arr = value.split("");
for (int i = 0, j = arr.length - 1; i < j; i++, j--) {
String temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
String[] newArr = new String[arr.length + (arr.length / 3 - (arr.length % 3 == 0 ? 1 : 0))];
for (int i = 0; i < arr.length; i++) {
newArr[i / 3 * 4 + i % 3] = arr[i];
if (i % 3 == 2 && i != arr.length - 1) {
newArr[i + 1 + i / 3] = ",";
}
}
for (int i = 0, j = newArr.length - 1; i < j; i++, j--) {
String temp = newArr[i];
newArr[i] = newArr[j];
newArr[j] = temp;
}
StringBuilder res = new StringBuilder();
for (String s : newArr) {
res.append(s);
}
value = res.toString();
}
return value;
}