你如何在java中转换为字符串时留下一个零填充int ?

我基本上是在寻找以前导零填充到9999的整数(例如1 = 0001)。


当前回答

下面是另一种将整数左侧填充为0的方法。您可以根据您的方便增加零的数量。已添加一个检查,以返回与配置的负数或大于或等于零的值相同的值。你可以根据你的要求进一步修改。

/**
 * 
 * @author Dinesh.Lomte
 *
 */
public class AddLeadingZerosToNum {
    
    /**
     * 
     * @param args
     */
    public static void main(String[] args) {
        
        System.out.println(getLeadingZerosToNum(0));
        System.out.println(getLeadingZerosToNum(7));
        System.out.println(getLeadingZerosToNum(13));
        System.out.println(getLeadingZerosToNum(713));
        System.out.println(getLeadingZerosToNum(7013));
        System.out.println(getLeadingZerosToNum(9999));
    }
    /**
     * 
     * @param num
     * @return
     */
    private static String getLeadingZerosToNum(int num) {
        // Initializing the string of zeros with required size
        String zeros = new String("0000");
        // Validating if num value is less then zero or if the length of number 
        // is greater then zeros configured to return the num value as is
        if (num < 0 || String.valueOf(num).length() >= zeros.length()) {
            return String.valueOf(num);
        }
        // Returning zeros in case if value is zero.
        if (num == 0) {
            return zeros;
        }
        return new StringBuilder(zeros.substring(0, zeros.length() - 
                String.valueOf(num).length())).append(
                        String.valueOf(num)).toString();
    }
}

输入

0

7

13

713

7013

9999

输出

0000

0007

0013

7013

9999

其他回答

如果你出于任何原因使用1.5之前的Java,那么可以尝试使用Apache Commons Lang方法

org.apache.commons.lang.StringUtils.leftPad(String str, int size, '0')

找到这个例子…将测试……

import java.text.DecimalFormat;
class TestingAndQualityAssuranceDepartment
{
    public static void main(String [] args)
    {
        int x=1;
        DecimalFormat df = new DecimalFormat("00");
        System.out.println(df.format(x));
    }
}

测试了这个和:

String.format("%05d",number);

对于我的目的,我认为这两个都可以。格式更好,更简洁。

下面是另一种将整数左侧填充为0的方法。您可以根据您的方便增加零的数量。已添加一个检查,以返回与配置的负数或大于或等于零的值相同的值。你可以根据你的要求进一步修改。

/**
 * 
 * @author Dinesh.Lomte
 *
 */
public class AddLeadingZerosToNum {
    
    /**
     * 
     * @param args
     */
    public static void main(String[] args) {
        
        System.out.println(getLeadingZerosToNum(0));
        System.out.println(getLeadingZerosToNum(7));
        System.out.println(getLeadingZerosToNum(13));
        System.out.println(getLeadingZerosToNum(713));
        System.out.println(getLeadingZerosToNum(7013));
        System.out.println(getLeadingZerosToNum(9999));
    }
    /**
     * 
     * @param num
     * @return
     */
    private static String getLeadingZerosToNum(int num) {
        // Initializing the string of zeros with required size
        String zeros = new String("0000");
        // Validating if num value is less then zero or if the length of number 
        // is greater then zeros configured to return the num value as is
        if (num < 0 || String.valueOf(num).length() >= zeros.length()) {
            return String.valueOf(num);
        }
        // Returning zeros in case if value is zero.
        if (num == 0) {
            return zeros;
        }
        return new StringBuilder(zeros.substring(0, zeros.length() - 
                String.valueOf(num).length())).append(
                        String.valueOf(num)).toString();
    }
}

输入

0

7

13

713

7013

9999

输出

0000

0007

0013

7013

9999

使用这个简单的扩展函数

fun Int.padZero(): String {
    return if (this < 10) {
        "0$this"
    } else {
        this.toString()
    }
}

Kotlin的

fun Calendar.getFullDate(): String {
    val mYear = "${this.get(Calendar.YEAR)}-"
    val mMonth = if (this.get(Calendar.MONTH) + 1 < 10) {
        "0${this.get(Calendar.MONTH) + 1}-"
    } else {
        "${this.get(Calendar.MONTH)+ 1}-"
    }
    val mDate = if (this.get(Calendar.DAY_OF_MONTH)  < 10) {
        "0${this.get(Calendar.DAY_OF_MONTH)}"
    } else {
        "${this.get(Calendar.DAY_OF_MONTH)}"
    }
    return mYear + mMonth + mDate
}

并将其用作

val date: String = calendar.getFullDate()