0

Numi app results

The attached diagram shows the correct results in Numi app in IOS. When i calculate the same calculation, it calculates differently. I want the same way as how Numi did on the image.

amount = 4;
commission = 0.5; 
round = 6;
result = amount / (1 + commission / 100);
fee = result * (commission / 100)
total = result + fee

Here the total value must be equal to the given amount for any input. Js results 4.000001 if i set decimal precision to 6

testFunction(4);

function testFunction(amount) {
  const commission = 0.5;
  const round = 6;

  const finalAmount = amount / (1 + commission / 100);
  const result = roundOff(finalAmount, round);
  const fee = roundOff(result * (commission/100), round);
  const total = roundOff(result + fee, round);
  if(amount > total || amount < total)
  console.log("Qty: " + result + " fee: " + fee + " Total = "+ total);
  return total;
}

function roundOff (x, n) {
 return Number(parseFloat(Math.round(x * Math.pow(10, n)) / Math.pow(10, n)).toFixed(n));
}
  • 1
    Also it is very likely that Numi is calculating the same value but sensibly accounting for floating point. see: [How to deal with floating point number precision in JavaScript?](https://stackoverflow.com/questions/1458633/how-to-deal-with-floating-point-number-precision-in-javascript) and [Is floating point math broken?](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) – pilchard Feb 08 '23 at 10:48
  • @pilchard Any idea about how to do the same one on js? – Praveen Kumar s Feb 08 '23 at 10:51

0 Answers0