Tom Parke
Tom Parke

Reputation: 119

Can the sum of a number, and a percentage of the same number rounded up be reversed in Javascript?

I'm trying to run a calculation backwards. The function for getting a number is:

const getPrice = (num, percentage) => num + ((Math.ceil(num/100)*100)*percentage)

getPrice(1000, 0.03) // returns 1,030
getPrice(32245, 0.03) // returns 33,214
getPrice(52143.23,0.03) // 53709.23

In plain terms it returns the sum of a number and a percentage of that number rounded up to the nearest hundred.

Is it possible to get the base price (the input, num) give the result of the function?

 const getBasePrice = (num, percentage) => {return insertLogicHere}

 getBasePrice(1030, 0.03) // returns 1000
 getBasePrice(33214, 0.03) // returns 32245
 getBasePrice(53709.23,0.03) // returns 52143.23

I know this is more of a math question than a purely Javascript question but I'm not sure how to format this for a Math audience. I haven't been able to figure this one out even with the help of some other programmers at work.

Upvotes: 0

Views: 82

Answers (3)

Tom Parke
Tom Parke

Reputation: 119

Okay so I called my mum and she helped me figure it out. There are some more specific variable names here and decimals are being fixed because we're working with dollars.

const getBasePrice = (num, percent) => {
  const guess = num / (1 + percent);
  const roundUp = Math.ceil(guess / 100) * 100;
  const stampDuty = roundUp * percent;
  return parseFloat((num - stampDuty).toFixed(2));
};

With tests:

const getPrice = (num, percentage) => {
  return num + Math.ceil(num / 100) * 100 * percentage;
}
const getBasePrice = (num, percent) => {
  const guess = num / (1 + percent);
  const roundUp = Math.ceil(guess / 100) * 100;
  const stampDuty = roundUp * percent;
  return parseFloat((num - stampDuty).toFixed(2));
};

const testFunction = () => {
  const percent = 0.03
  const number = parseFloat((Math.random() * 100000).toFixed(2))
  const stampDutyPrice = getPrice(number,percent)
  const backNumber = getBasePrice(stampDutyPrice,percent)
  
  return number === backNumber
}

for (let i = 0; i < 100; i++) {
  console.log(testFunction() ? 'test passed' : 'text failed')
}

Upvotes: 0

SBartlett97
SBartlett97

Reputation: 91

EDIT: By using the existing getPrice method you can work out the difference between the original and new values and reverse it exactly:

const getPrice = (num, percentage) => num + ((Math.ceil(num/100)*100)*percentage)
const getBasePrice = (num, percentage) => {return Math.floor(num/(1+percentage))}

let num = 53709.23
let percentage = 0.03

let initialReverse = getBasePrice(num, percentage)
let finalReverse = initialReverse - (getPrice(initialReverse, percentage) - num)

console.log(finalReverse) //returns 52143.23

Upvotes: 1

Rub&#233;n Vega
Rub&#233;n Vega

Reputation: 730

Like this, however you will have differences because of rounding.

const getBasePrice = (num, percentage) => {return (Math.round((100*num)/(((percentage*100)+100))))}

console.log(getBasePrice(1030, 0.03)) // returns 1000
console.log(getBasePrice(33214, 0.03)) // returns 32245

EDIT

If you remove the rounding is and exact match. Maybe you could do it like this when you are making the opperations and apply a format latter when you want to show this values.

const getPrice = (num, percentage) => num + (num/100)*100*percentage

console.log(getPrice(1000, 0.03)) // returns 1,030
console.log(getPrice(32245, 0.03)) // returns 33212.35
console.log(getPrice(1244.124, 0.03)) // returns 1281.4477200000001


const getBasePrice = (num, percentage) => ((100*num)/(((percentage*100)+100)))

console.log(getBasePrice(1030, 0.03)) // returns 1000
console.log(getBasePrice(33212.35, 0.03)) // returns 32245
console.log(getBasePrice(1281.4477200000001, 0.03)) // returns 1244.124

Upvotes: 0

Related Questions