你如何在java中转换为字符串时留下一个零填充int ?
我基本上是在寻找以前导零填充到9999的整数(例如1 = 0001)。
你如何在java中转换为字符串时留下一个零填充int ?
我基本上是在寻找以前导零填充到9999的整数(例如1 = 0001)。
当前回答
您需要使用Formatter,下面的代码使用NumberFormat
int inputNo = 1;
NumberFormat nf = NumberFormat.getInstance();
nf.setMaximumIntegerDigits(4);
nf.setMinimumIntegerDigits(4);
nf.setGroupingUsed(false);
System.out.println("Formatted Integer : " + nf.format(inputNo));
输出:0001
其他回答
如果性能在您的情况下很重要,您可以自己做,与String相比开销更少。格式功能:
/**
* @param in The integer value
* @param fill The number of digits to fill
* @return The given value left padded with the given number of digits
*/
public static String lPadZero(int in, int fill){
boolean negative = false;
int value, len = 0;
if(in >= 0){
value = in;
} else {
negative = true;
value = - in;
in = - in;
len ++;
}
if(value == 0){
len = 1;
} else{
for(; value != 0; len ++){
value /= 10;
}
}
StringBuilder sb = new StringBuilder();
if(negative){
sb.append('-');
}
for(int i = fill; i > len; i--){
sb.append('0');
}
sb.append(in);
return sb.toString();
}
性能
public static void main(String[] args) {
Random rdm;
long start;
// Using own function
rdm = new Random(0);
start = System.nanoTime();
for(int i = 10000000; i != 0; i--){
lPadZero(rdm.nextInt(20000) - 10000, 4);
}
System.out.println("Own function: " + ((System.nanoTime() - start) / 1000000) + "ms");
// Using String.format
rdm = new Random(0);
start = System.nanoTime();
for(int i = 10000000; i != 0; i--){
String.format("%04d", rdm.nextInt(20000) - 10000);
}
System.out.println("String.format: " + ((System.nanoTime() - start) / 1000000) + "ms");
}
结果
自带功能:1697ms
字符串。格式:38134毫秒
假设你想把11打印成011
您可以使用格式化程序:“%03d”。
你可以像这样使用这个格式化器:
int a = 11;
String with3digits = String.format("%03d", a);
System.out.println(with3digits);
另外,一些java方法直接支持这些格式化器:
System.out.printf("%03d", a);
不需要软件包:
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
找到这个例子…将测试……
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);
对于我的目的,我认为这两个都可以。格式更好,更简洁。