cdw100100
cdw100100

Reputation: 1

getting my jquery calculator to be able to calculate percentages

The problem is my simple jquery calculator I am having trouble finding out how to get it to do percentages like the simple one on windows. I have had alot of trouble writing the code and I have had to scrap alot of it and try again does anyone have a simple solution.

Upvotes: 0

Views: 126

Answers (1)

piticent123
piticent123

Reputation: 323

What the percent button does is it takes the last number entered, and it also takes the percentage entered. It then interprets the latter as that percentage of the former. If this is very confusing (as it's worded rather weirdly), here it is in numbers:

Input:
X + Y%

What the Calculator Thinks:
X + X * Y%

For example, if I enter in 20 + 50%, the calculator takes 20, and interprets 50% as 50% of 20, or 10. Therefore, the answer would be 30.

To conclude:
1. Get the percentage
2. Get the number directly before it
3. Interpret #2 as that percent of #1 (#2 = #1 * (#2 / 100))

Hopefully, this gives you enough of an idea to work some jQuery magic. Good luck!

Upvotes: 1

Related Questions