Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
433 views
in Technique[技术] by (71.8m points)

java - 如何在左边填充零的整数?(How can I pad an integer with zeros on the left?)

How do you left pad an int with zeros when converting to a String in java?

(你怎么离开垫的int转换成时用零String在java吗?)

I'm basically looking to pad out integers up to 9999 with leading zeros (eg 1 = 0001 ).

(我基本上是希望用前导零填充最大9999整数(例如1 = 0001 )。)

  ask by Omar Kooheji translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use java.lang.String.format(String,Object...) like this:

(像这样使用java.lang.String.format(String,Object...) :)

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

for zero-padding with a length of 5. For hexadecimal output replace the d with an x as in "%05x" .

(用于长度为5的零填充。对于十六进制输出,将d替换为x"%05x" 。)

The full formatting options are documented as part of java.util.Formatter .

(完整的格式设置选项作为java.util.Formatter一部分进行了说明。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...