不可以,那样的话就和前一个数字的位数有关而我要的是和前一个数字的位数无关,不管多少位,占有的空间是一样的
不过,还是谢谢你!
可以自己来做啊
class test
{
final static int NUM_LEN = 4;
public static void main (String args[]) throws Exception
{
System.out.println(format(2) + format(300) + "\r\n" + format(10) + format(20));
}
static String format(int num) throws Exception
{
String tmp = String.valueOf(num);
int len = tmp.length();
if (len > NUM_LEN)
throw new Exception("Number value out of range");
int diff = NUM_LEN - len;
for (int i=0; i<diff; i++)
tmp = " "+tmp;
return tmp;
}
}
{
final static int NUM_LEN = 4;
public static void main (String args[]) throws Exception
{
System.out.println(format(2) + format(300) + "\r\n" + format(10) + format(20));
}
static String format(int num) throws Exception
{
String tmp = String.valueOf(num);
int len = tmp.length();
if (len > NUM_LEN)
throw new Exception("Number value out of range");
int diff = NUM_LEN - len;
for (int i=0; i<diff; i++)
tmp = " "+tmp;
return tmp;
}
}