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

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


当前回答

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()

其他回答

虽然上面的许多方法都很好,但有时我们需要格式化整数和浮点数。我们可以使用这个方法,特别是当我们需要在小数的左右填充特定数量的0时。

import java.text.NumberFormat;  
public class NumberFormatMain {  

public static void main(String[] args) {  
    int intNumber = 25;  
    float floatNumber = 25.546f;  
    NumberFormat format=NumberFormat.getInstance();  
    format.setMaximumIntegerDigits(6);  
    format.setMaximumFractionDigits(6);  
    format.setMinimumFractionDigits(6);  
    format.setMinimumIntegerDigits(6);  

    System.out.println("Formatted Integer : "+format.format(intNumber).replace(",",""));  
    System.out.println("Formatted Float   : "+format.format(floatNumber).replace(",",""));  
 }    
}  

试试这个:

import java.text.DecimalFormat; 

DecimalFormat df = new DecimalFormat("0000");

String c = df.format(9);   // Output: 0009

String a = df.format(99);  // Output: 0099

String b = df.format(999); // Output: 0999

下面是如何不使用DecimalFormat格式化字符串的方法。

字符串。格式(“% 2 d”,9)

09

字符串。格式(“% d 03”,19)

019

字符串。格式(“% 04 d”,119年)

0119

不需要软件包:

String paddedString = i < 100 ? i < 10 ? "00" + i : "0" + i : "" + i;

这将把字符串填充到三个字符,并且很容易为四个或五个字符添加更多的部分。我知道这在任何方面都不是完美的解决方案(特别是如果你想要一个大的填充字符串),但我喜欢它。

下面是另一种将整数左侧填充为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