Oma*_*eji 964 java formatting zero-pad
int
转换为String
java 时,如何使用零填充?
我基本上希望9999
用前导零填充整数(例如1 = 0001
).
Yon*_*oit 1627
使用java.lang.String.format(String,Object...)
这样:
String.format("%05d", yournumber);
Run Code Online (Sandbox Code Playgroud)
用于填充零为5的长度十六进制输出替换d
用x
如在"%05x"
.
完整的格式选项记录为一部分java.util.Formatter
.
Bor*_*vić 111
如果您因任何原因使用pre 1.5 Java,那么可以尝试使用Apache Commons Lang方法
org.apache.commons.lang.StringUtils.leftPad(String str, int size, '0')
Run Code Online (Sandbox Code Playgroud)
bvd*_*vdb 101
假设您要打印11
为011
你可以使用格式化程序:"%03d"
.
您可以像这样使用此格式化程序:
int a = 11;
String with3digits = String.format("%03d", a);
System.out.println(with3digits);
Run Code Online (Sandbox Code Playgroud)
或者,一些java方法直接支持这些格式化程序:
System.out.printf("%03d", a);
Run Code Online (Sandbox Code Playgroud)
Oma*_*eji 27
找到这个例子......将测试......
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));
}
}
Run Code Online (Sandbox Code Playgroud)
测试过这个和:
String.format("%05d",number);
Run Code Online (Sandbox Code Playgroud)
两者都有效,我认为String.Format更好更简洁.
das*_*eks 19
如果性能在您的情况下很重要,那么与String.format
功能相比,您可以用更少的开销自己完成:
/**
* @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();
}
Run Code Online (Sandbox Code Playgroud)
性能
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");
}
Run Code Online (Sandbox Code Playgroud)
结果
自己的功能: 1697ms
String.format: 38134ms
Tho*_*Tho 15
您可以使用Google Guava:
Maven的:
<dependency>
<artifactId>guava</artifactId>
<groupId>com.google.guava</groupId>
<version>14.0.1</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
示例代码:
String paddedString1 = Strings.padStart("7", 3, '0'); //"007"
String paddedString2 = Strings.padStart("2020", 3, '0'); //"2020"
Run Code Online (Sandbox Code Playgroud)
注意:
Guava
是非常有用的图书馆,还提供了大量的功能,这关系到Collections
,Caches
,Functional idioms
,Concurrency
,Strings
,Primitives
,Ranges
,IO
,Hashing
,EventBus
,等
参考:Guava解释
小智 10
试试这个:
import java.text.DecimalFormat;
DecimalFormat df = new DecimalFormat("0000");
String c = df.format(9); // 0009
String a = df.format(99); // 0099
String b = df.format(999); // 0999
Run Code Online (Sandbox Code Playgroud)
以下是不使用DecimalFormat
.
String.format("%02d", 9)
09
String.format("%03d", 19)
019
String.format("%04d", 119)
0119
归档时间: |
|
查看次数: |
596806 次 |
最近记录: |