java数字千分位格式转换

2023-12-13 17:59:52

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;
    }

文章来源:https://blog.csdn.net/weixin_47315082/article/details/134835819
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。