Javascript decimal calculation for the pre assume value as like Numi app

71 Views Asked by At

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));
}

0

There are 0 best solutions below