I would like to create a javascript function which will round up any 2 decimal place number to the next whole number divisible by itself.
So
3.50 >= 7
4.50 >= 9
2.25 >= 9
etc
I would like to create a javascript function which will round up any 2 decimal place number to the next whole number divisible by itself.
So
3.50 >= 7
4.50 >= 9
2.25 >= 9
etc
This sounds like a algorithm question. basic idea: for a given number n, take the parts after the dot, times 100, try to find its common divisor, see how many 2, and 5s inside. U need to provide some additional 2, or 5, to make it into multiples of 100.
For example, n = 2.25, take .25 * 100 = 25 = 5*5.
Therefore, you need additional 2*2 to make it 100.
Thus the answer is: 2.25 * 2*2 = 9
Update: but frankly speaking I think this kind of algorithm question should be asked somewhere else.. any suggestions upon where to ask?
So, you have a problem coming up with a algorithm to get your desired result. What steps should you take?
First of all, see if you can find a formula to get the desired output. In this case, that doesn't appear to be that simple.
Are there other ways to get the desired result?
You're basically looking for a multiplication of your starting number (start
). Would it be possible have some kind of loop increase the multiplier, untill we find the result?
Yes.
The steps your code should take are, basically:
For a starting number, `start` (2.25),
And a multiplier, `i`, starting at 1,
While `start * i` is not a integer:
Increment `i`.
Then, start * i
is your result.