user2988129
user2988129

Reputation: 301

Java add 000 to end of int

I'm trying to add a load of zeros to the end of an int.

I have a for loop that passes i to a method.

So when i is passed to the method I want to say i+000 to i+999

I've tried

int i = 1;
int j = 000;
int k = Integer.valueOf(String.valueOf(i) + String.valueOf(j));
System.out.println(k);

But this just prints out 10

Upvotes: 0

Views: 353

Answers (2)

musefan
musefan

Reputation: 48465

Just to clarify why your attempt doesn't work. It's because int j = 000; will jut set the int to 0, this will naturally become the string "0" which is why you get "10" when you add the two strings together.

To offer a modification to your attempt (though I would go with Petter's solution personally), you can just use a fixed string:

int i = 1;
String j = "000";
int k = Integer.valueOf(String.valueOf(i) + j);
System.out.println(k);

Upvotes: 3

Petter
Petter

Reputation: 4165

Why not just use int k = i * 1000?

Upvotes: 10

Related Questions