1

Currently im using code to round up as you can see here:

outputs[0].textContent = Math.ceil(value / 0.7);
    outputs[1].textContent = Math.ceil(value * 1);
    outputs[2].textContent = Math.ceil(value / 0.7 - (value / 0.7 / 100 * 40));
    outputs[3].textContent = Math.ceil(value * 1);
    outputs[4].textContent = Math.ceil(value / 0.7 - (value / 0.7 / 100 * 20));
    outputs[5].textContent = Math.ceil(value / 0.7 - (value / 0.7 / 100 * 10));

However this is not always rounding the to integer above. Is there any other way for the outputs to automatically be rounded to the integer above?

Lewis
  • 29
  • 7
  • Do you have an example? `Math.ceil(0.1)` returns `1`. Why do you calculate `value * 1`? Does it help if you rewrite `value / 0.7 - (value / 0.7 / 100 * 40)` to `value * (1 / 0.7 - 1 / 0.7 / 100 * 40)`? – jabaa Aug 25 '21 at 08:23
  • I am following a calculator from excel and i have recreated it - however some of the outputs return differently im trying to find an example, but i dont know how to share images? – Lewis Aug 25 '21 at 08:28
  • Don't shares image. Just give a value for `value`. You could also try `value * (1 - 1 / 100 * 40) / 0.7` – jabaa Aug 25 '21 at 08:29
  • @jabaa if i were to enter the price of 100.6 the excel calculator returns the value of £116 where as the one i created returns the value of £115, even though i have used Math.ceil() – Lewis Aug 25 '21 at 08:30
  • @jabaa the only reason i have the outputs calculating as such is because im following the same rules as is on excel - i'll give yours a go quickly – Lewis Aug 25 '21 at 08:32
  • Yes, and you found out it doesn't work and now we're looking for the reason and a solution. – jabaa Aug 25 '21 at 08:32
  • Yeah, i tried using what you recommended and it returned the wrong value also – Lewis Aug 25 '21 at 08:34
  • 2
    Please provide a [mcve]. A simpler form for `value / 0.7 - (value / 0.7 / 100 * 40)` is `value * 6 / 7`. – jabaa Aug 25 '21 at 08:35
  • This is the formula from excel - =ROUNDUP(D8-(D8/100*40),0) - im not sure how i would be able to simplify that and still get the same output – Lewis Aug 25 '21 at 08:37
  • `D8-(D8/100*40)` is `D8 * 3 / 5`. Your question is probably a duplicate of [Is floating point math broken?](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) but it's unclear without more details. – jabaa Aug 25 '21 at 08:38
  • i just tried using the simpler value and its starting to round up like it should - thank you for your help :) – Lewis Aug 25 '21 at 08:42

0 Answers0