Varun Agarwal
Varun Agarwal

Reputation: 1587

Generate a random number and add 0 before it to ensure fixed number of digits

so I am trying to generate a random number. I am going to append the day and month as integers before the random number. This I am able to do by using the following code.

    Calendar calendar;
    calendar=Calendar.getInstance();
    int day= calendar.get(Calendar.DAY_OF_MONTH);
    int month=calendar.get(Calendar.MONTH)+1; //it treats Jan as 0 hence i add 1
    int num= Integer.valueOf(String.valueOf(month)+String.valueOf(day));

Now i need to generate a random number but add 0s before it. For example today is 21st September so numbers will look like

    921 (num) + 22334 (random num) = 92122334
    921 (num) + 2 (random num) = 92100002

Basically add 0s to the start ensuring number of digits remain the same. The use case of this is an easier way of generating unique order numbers that have an inbuilt time stamp as well. I dont expect to process more than 200 orders a day hence taking a 5 digit random number seems reasonable enough for probability of duplicates to be very small.

Upvotes: 2

Views: 3088

Answers (5)

SubOptimal
SubOptimal

Reputation: 22983

Two possible solutions.

Calendar calendar = GregorianCalendar.getInstance();
int num = 0;
num += (calendar.get(Calendar.MONTH) + 1) * 10_000_000;
num += calendar.get(Calendar.DAY_OF_MONTH) * 100_000;
num += your_random_number_lower_100000

second

Calendar calendar = GregorianCalendar.getInstance();
String randomDigits = String.format("%d%02d%05d", 
        calendar.get(Calendar.MONTH) + 1,
        calendar.get(Calendar.DAY_OF_MONTH),
        your_random_number_lower_100000
);

Upvotes: 3

Vishesh Chandra
Vishesh Chandra

Reputation: 7071

You can use String format:

String finalString = String.format("%d%05d", num, randomNum);

Here you can pass first parameter, Your calculation of day and month and second parameter Random number that got.

%05d means: If your integer number digit size will be less than 5 then it will append the 0 (zero) to make the number in 5 digit.

Upvotes: 1

Marcus Hooper
Marcus Hooper

Reputation: 657

If you are going to store this as a string (which it seems you are already doing since you are adding 921 and 22334 and getting 92122334?) the easiest way would be to add 0's to the start of the string until it reaches a certain length.

StringBuilder numberString = new StringBuilder(maxSize); numberString.append(yourNumber);

while(numberString.length() < maxSize) numberString.insert('0');

Upvotes: 0

Eli Blokh
Eli Blokh

Reputation: 12293

You could construct a String by such way:

 String value = String.valueOf(randomNum); //your random num should have 5 digits or less
 int N = 5; //your length
 while (value.length < N)
   value = "0" + value;

Upvotes: 0

Niki van Stein
Niki van Stein

Reputation: 10734

What about

int newnumber = 921*100000 + 2;

that gives 92100002. instead of 2 you can use any random number of course. If you want to add zeros in front of the 921 so it becomes 0921 and for 1st September 0901 for example, you need to convert it to a string and check the length, and add zeros till you have the length that you want.

Upvotes: 0

Related Questions