0

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

user1002794
  • 123
  • 1
  • 11
  • 2
    Sharing your research helps everyone. Tell us what you've tried and why it didn't meet your needs. This demonstrates that you've taken the time to try to help yourself, it saves us from reiterating obvious answers, and most of all it helps you get a more specific and relevant answer! Also see [how to ask](http://stackoverflow.com/questions/how-to-ask) – Cerbrus Jul 23 '15 at 08:59
  • possible duplicate of http://stackoverflow.com/questions/14783869/convert-a-decimal-number-to-a-fraction-rational-number – Jaromanda X Jul 23 '15 at 09:06

2 Answers2

0

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?

songyy
  • 4,323
  • 6
  • 41
  • 63
0

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.

Cerbrus
  • 70,800
  • 18
  • 132
  • 147