本文主要是介绍java数字千分位格式转换,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
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;}
这篇关于java数字千分位格式转换的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!