java 在java中生成序列号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5387031/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 10:53:04  来源:igfitidea点击:

Generating sequence number in java

java

提问by vignesh

I want to generate a sequence number starting from 00000001,00000002....(i need all these zero as well)Please help me..Thanks in advance..

我想生成一个从 00000001,00000002 开始的序列号......(我也需要所有这些零)请帮助我..提前谢谢..

回答by Kai G

You can just generate the sequence numbers as integers and then format them like this:

您可以将序列号生成为整数,然后像这样格式化它们:

String.format("%08d", yournumber);

See this question: How can I pad an integers with zeros on the left?

看到这个问题: 我怎样才能在左边用零填充整数?

回答by paxdiablo

You can just maintain that variable as an integer and use String.formatto format it.

您可以将该变量保持为整数并用于对其String.format进行格式化。

String s = String.format ("%08d", 42); // gives you "00000042"

回答by WhiteFang34

You just need to use string formattingto pad numbers with zeros:

您只需要使用字符串格式用零填充数字:

for (int i = 1; i < 1000; i++) {
    String sequence = String.format("%08d", i);
}