用Java将两位数格式的整数保存在变量中

用Java将两位数格式的整数保存在变量中,java,numbers,Java,Numbers,如何在Java中存储两位数格式的整数?我能做什么 int a=01; 并将其打印为01?而且,如果我说intb=a,不仅是打印,b还应将其值打印为01这是不可能的,因为整数是整数。但是,如果需要(),您可以格式化整数。我想这就是您想要的: int a = 1; DecimalFormat formatter = new DecimalFormat("00"); String aFormatted = formatter.format(a); System.out.println(aForma

如何在Java中存储两位数格式的整数?我能做什么

int a=01;

并将其打印为
01
?而且,如果我说
intb=a,不仅是打印
b
还应将其值打印为
01

这是不可能的,因为整数是整数。但是,如果需要(),您可以格式化整数。

我想这就是您想要的:

int a = 1;
DecimalFormat formatter = new DecimalFormat("00");
String aFormatted = formatter.format(a);

System.out.println(aFormatted);
或者,更简单地说:

int a = 1;
System.out.println(new DecimalFormat("00").format(a));
int只存储一个数量,01和1表示相同的数量,因此它们的存储方式相同


构建一个表示特定格式数量的字符串。

查看下面的格式,上面的格式对我适用

// below, %02d says to java that I want my integer to be formatted as a 2 digit representation
String temp = String.format("%02d", yourIntValue);
// and if you want to do the reverse
int i = Integer.parse(temp);

// 2 -> 02 (for example)

System.out.printf(“%02d”,myNumber)

整数是整数。假设没有八进制表示法(Java文本中有八进制表示法),那么1=01=001=。。您希望将整数转换为该格式的字符串。首先,int可以表示比99大得多的值。如果您需要这种表示,请创建您自己的类。我想您正在寻找类似这样的内容:添加了[0的可能副本,但在转换为int时不会显示为两位数]()@Mist4u,您是否使用过COBOL?:-数据应该是“%02d”而不是“%2d”