我有一个整数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. 如何在javascript中添加填充 - 左?
- 3. 用数字fmt.Printf添加零填充
- 4. 如何在C++中将填充添加到BYTE数组中?
- 5. 将填充添加到文本字段的“左边”
- 6. 如何添加填充到java swt组
- 7. 如何将零添加到数字
- 8. 如何让MaskedTextBox左边用零填充数字?
- 9. 在swift中添加填充到UITextField的左边距
- 10. 右填充或零填充左对齐的字符串
- 11. 如何将PKCS7填充添加到rijndael.h?
- 12. 如何将图像添加到ListView从数据库中填充
- 13. 在Julialang中带有前导(左填充)零的字符串的数字
- 14. 如何在Python中向零填充数字字符串?
- 15. 如何用零填充数字?
- 16. 如何在python中添加填充图?
- 17. 如何在WebView中添加填充
- 18. 如何将新行添加到由IDataReader填充的表中?
- 19. 用零填充数字
- 20. Java中的微调零填充
- 21. 用零填充列表中的Java
- 22. Edittext如何将字符移动到左侧如果填充
- 23. 如何将填充字节添加到位图?
- 24. 如何在Java中填充数组?
- 25. 如何将填充添加到在d3中用作滑块的画笔中?
- 26. Java不添加PKCS5填充
- 27. 将“填充”添加到div的外部?
- 28. 如何在java中使用左填充帐号
- 29. JQuery添加填充到原始填充
- 30. 在最后一位数中填充零
'8位'(而不是'8位数')会更准确 – leonbloy 2010-04-24 16:19:20