数字以0补全

public static void main(String[] args) {
        NumberFormat formatter1 = NumberFormat.getNumberInstance();
        //5位数,前面补0
        formatter1.setMinimumIntegerDigits(5);
        formatter1.setGroupingUsed(false);
        String format = formatter1.format(56);
        System.out.println(format); //00056
    }

 方法2:

public static String getNewFileName(String oldName, int len) {
        StringBuffer buffer = new StringBuffer(oldName);
        while (buffer.length() < len) {
            buffer.insert(0, 0); // 补0
        }
        return buffer.toString();
    }

    public static void main(String[] args) {
        System.out.println(getNewFileName("56", 6));
    }

 

posted @ 2020-08-11 14:01  静默之尘  阅读(109)  评论(0编辑  收藏  举报