User3
User3

Reputation: 2535

Division to yield result in multiples of hundred or thousand etc

I have a variable set in my program:

int weekInterval = 100; 

I have the following function:

public void weeksToNotify(int weeks, String date){
        Date dt = Convert(date); 
        Calendar cal = Calendar.getInstance(); 
        cal.setTime(dt); 
        weekSpecial = weekInterval*(1+(weeks/weekInterval)); 
        cal.add(Calendar.DATE, (weekSpecial*7));
        dateWeekReg = cal.getTime();
    }

The parameters to this function are:

int weeks: The current age in weeks for the person/event/thing.

String date: The date of birth of the person/event/thing.

Now upon execution of this function the variable weekSpecial has the desired value, but I want to round off this value. So for example if the weekSpecial variable has the value 150 I want 200 as the result, if it has a value of 23400 I want only 23000 etc. I am not looking for a precise answer, I want to learn how to solve this. SO a pointer or a hint/ Algo will do for me. Thanks :)

Upvotes: 1

Views: 1431

Answers (4)

Am_I_Helpful
Am_I_Helpful

Reputation: 19158

A possible algorithm for this can go like this :-

Check the length of your WeekSpecial variable(first converting to String and then applying int length() method).

See,if WeekSpecial Variable is in range of 100's--->then do weekSpecial = weekSpecial/100 * 100.

If WeekSpecial Variable is in range of 1000's--->then do weekSpecial = weekSpecial/1000 * 1000.

Similarly,if WeekSpecial Variable is in the range of 10^n,---> then do weekSpecial=(weekSpecial/10^n)*10^n.

Also,if you round off the value to nearest power 10^n go as shown below :-

Say,160-->200------(1.0*160/100)=1.6.Apply,Math.round(1.6)=2. and after rounding,multiply it by 10^n,i.e.,100 here,you'll get 200 here.I hope I am clear!

Probably,this could help you by rounding off the value!

Upvotes: 2

CocoNess
CocoNess

Reputation: 4222

You could use Math.round. This rounds your number to the nearest value, above or below your integer. The below example rounds to the nearest 100.

int weekInterval;

System.out.println (Math.round(weekInterval/100.00)*100);

If you want to round to the nearest 1000, simply divide/multiply by 1000.

Upvotes: 2

PNS
PNS

Reputation: 19905

For the first value (150), you can try setting

weekSpecial = weekSpecial/100 * 100

after the calculation of the weekSpecial parameter.

Integer division in Java drops the decimal part, so first dividing and then multiplying by 100 should give you what you asked.

Of course, the calculation can be integrated in one statement, e.g.

weekSpecial = (weekInterval*(1+(weeks/weekInterval)) / 100) * 100;

This works if the rounding you need is 2 digits (e.g., from 150 to 100), otherwise you need to divide and multiply by 10 ^ n, where n is the number of digits you need rounded off (in the case of 150, n = 2).

So, for 23500 the multiplication should be 23500/1000 * 1000.

In general, assuming that you know n, the answer is

double factor = Math.pow(10, n);
weekSpecial = (int)(Math.round(weekSpecial/factor * factor));

Upvotes: 2

Shlomo Koppel
Shlomo Koppel

Reputation: 945

From what it seems you are looking for an algorithm that starts rounding accorging to the second character from the left.

since you don't want an answer I will hint that if you divide the value of weekSpecial by 10 (with a counter) until the value is less than 100 and then divide by 100 and use the Round function, and finally multiply by 10^(2 + counter) you should be able to get the rounded number you're looking for.

Good luck!

Upvotes: 3

Related Questions