我有一个整数100
,我如何格式化它看起来像00000100
(总是8位数字)?如何在Java中将左填充的零添加到数字中?
回答
试试这个:
String formattedNumber = String.format("%08d", number);
每次它被称为一个新的formater实例我创建。所以我不得不承认,大量的数据会导致大的内存问题。 – 2017-09-25 13:23:54
您还可以使用类DecimalFormat
,像这样:
NumberFormat formatter = new DecimalFormat("00000000");
System.out.println(formatter.format(100)); // 00000100
String.format
使用其描述here
这也适用于一个格式字符串 :
int i = 53;
String spaceHolder = "00000000";
String intString = String.valueOf(i);
String string = spaceHolder.substring(intString.lenght()).contract(intString);
但是其他的例子要容易得多。
又一种方式。 ;)
int x = ...
String text = (""+(500000000 + x)).substring(1);
-1 => 99999999(九补)
import java.util.concurrent.Callable;
/* Prints.
String.format("%08d"): Time per call 3822
(""+(500000000+x)).substring(1): Time per call 593
Space holder: Time per call 730
*/
public class StringTimer {
public static void time(String description, Callable<String> test) {
try {
// warmup
for(int i=0;i<10*1000;i++)
test.call();
long start = System.nanoTime();
for(int i=0;i<100*1000;i++)
test.call();
long time = System.nanoTime() - start;
System.out.printf("%s: Time per call %d%n", description, time/100/1000);
} catch (Exception e) {
System.out.println(description+" failed");
e.printStackTrace();
}
}
public static void main(String... args) {
time("String.format(\"%08d\")", new Callable<String>() {
int i =0;
public String call() throws Exception {
return String.format("%08d", i++);
}
});
time("(\"\"+(500000000+x)).substring(1)", new Callable<String>() {
int i =0;
public String call() throws Exception {
return (""+(500000000+(i++))).substring(1);
}
});
time("Space holder", new Callable<String>() {
int i =0;
public String call() throws Exception {
String spaceHolder = "00000000";
String intString = String.valueOf(i++);
return spaceHolder.substring(intString.length()).concat(intString);
}
});
}
}
+1,尽管缓慢的解决方案:-)。尽管如此, – 2010-04-24 22:29:59
不适用于底片。 – polygenelubricants 2010-04-25 05:53:16
您可能会发现它不像其他解决方案那么慢,并且行为未定义为否定。就像其他解决方案一样,将会有一个非DDDDDDDD,其中D是一个数字,输出负数。 – 2010-04-25 12:00:06
如果您需要解析字符串,或支持国际化考虑延长
java.text.Format
对象。使用其他答案来帮助你获得格式。
如果你只需要打印出来,这是一个较短的版本:
System.out.printf("%08d\n", number);
如果谷歌番石榴是一个选项:
String output = Strings.padStart("" + 100, 8, '0');
或者阿帕奇共享郎咸平:
String output = StringUtils.leftPad("" + 100, 8, "0");
- 1. 如何将填充零添加到写入流的数字中?
- 2. 将零填充添加到字符串
- 3. 如何在javascript中添加填充 - 左?
- 4. 用数字fmt.Printf添加零填充
- 5. 如何在C++中将填充添加到BYTE数组中?
- 6. 将填充添加到文本字段的“左边”
- 7. 如何填充数字字段的零?
- 8. 删除SSIS中的左填充零
- 9. 如何添加填充到java swt组
- 10. 如何将零添加到数字
- 11. Java - 将填充添加到大图像
- 12. 如何让MaskedTextBox左边用零填充数字?
- 13. 如何将前导零的数字填充到固定长度?
- 14. 在swift中添加填充到UITextField的左边距
- 15. iOS添加左填充到UILabel
- 16. 右填充或零填充左对齐的字符串
- 17. 如何将PKCS7填充添加到rijndael.h?
- 18. Reg Ex左填充零点
- 19. 如何在scipy.fftpack中为二维数组填充零填充?
- 20. 如何将图像添加到ListView从数据库中填充
- 21. 如何将填充添加到字节数组?
- 22. 如何在Python中向零填充数字字符串?
- 23. 在Julialang中带有前导(左填充)零的字符串的数字
- 24. 将填充添加到svg
- 25. 如何在jQuery中创建零填充的随机数字?
- 26. 如何在MySQL中填充零的数字?
- 27. 如何用零填充数字?
- 28. 如何在WebView中添加填充
- 29. 如何在python中添加填充图?
- 30. 如何将新行添加到由IDataReader填充的表中?
'8位'(而不是'8位数')会更准确 – leonbloy 2010-04-24 16:19:20