简体   繁体   English

Java... 如何生成一个 4 位整数

[英]Java... How to make a 4 digit integer

I want to make an integer value from the range 1-9999.我想创建一个 1-9999 范围内的整数值。 But the integer values should be always in 4 digits.但整数值应始终为 4 位数字。

Like 0001, 0002, 0015, 0156, 1578像 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.这将始终在 01 或 1 等数字的左侧显示缺失的 0。

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

if you want to print 4 digit integer, use like this如果要打印 4 位整数,请像这样使用

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() 的更多详细信息

使用

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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