简体   繁体   中英

Java... How to make a 4 digit integer

I want to make an integer value from the range 1-9999. But the integer values should be always in 4 digits.

Like 0001, 0002, 0015, 0156, 1578

You can probably use String format of the integer you willing to, like this:

String.format("%04d", your integer));

This will always show missing 0 on the left on digits like 01 or 1.

System.out.format("%04d%n", n);

if you want to print 4 digit integer, use like this

System.out.printf("%04d%n",your integer);

if you want to store in a variable, use like this

String str=String.format("%04d", your integer));

Read more details about String format()

使用

String.format("%04d", your integer));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM