Reputation: 2032
i have a function that generates 2 random numbers and determine the number with the highest number of digits. What i want is to add zeros in the number with lower digits to make them equal. how can i do that? for example First number: 340 Second Number: 3
i want the result to be 340 and 003.
Upvotes: 1
Views: 44
Reputation:
you can use System.out.printf("%03d", valueName);
what happens is that any value that will be printed would be at least 3 numbers in length and it will print preceding 0s.
Upvotes: 1
Reputation: 5460
int lengthOfBigger = String.valueOf(biggerNumber).length();
String pattern = "%0" + lengthOfBigger + "d";
String smallerNumberPadded = String.format(pattern, smallerNumber);
Taken mostly from this question
EDIT:
For just spaces change pattern to
String pattern = "% " + lengthOfBigger + "d";
Upvotes: 3