Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding percentage of previous value #304

Closed
duprasf opened this issue Mar 20, 2015 · 9 comments
Closed

Adding percentage of previous value #304

duprasf opened this issue Mar 20, 2015 · 9 comments
Labels

Comments

@duprasf
Copy link

duprasf commented Mar 20, 2015

I was wondering if this feature can be easily added to this already great library. I often have to add a percentage to a value, the strings are usually stored in a database and would look something like this:
"(value)+25%"

When the operation is not using percents, I replace the "(value)" with the value I need and eval it. But I can't use the "+25%" part, I have to change it to
"(value)*1.25"

I can then change the "(value)" to the needed value and then run the math.eval() function. The big problem with this approach is that is that is does not always respect the operation order, for example "3+5+10%" should equal 8.8, but "3+5*1.1" equal 8.5.

Also, since I'm not always the one entering those formulas in the database, I can't change the formulas. I was hoping this functionality can be added to the mathjs library. I started to look at the uncompressed code but though it might be easier just to ask you.

@josdejong
Copy link
Owner

Having support for percentages would be nice. It's a bit tricky though, as the % character is already in use for modulus.

Maybe we could create percentage as a unit, so you could do something like value + 25 percent and/or value + 25 pct.

@duprasf
Copy link
Author

duprasf commented Mar 24, 2015

I did not think of modulus. Your solution would work, I would only have to regex replace any % that are not followed by a digit into percent or pct (depending what you implement) before feeding it to math.eval.

@josdejong
Copy link
Owner

There may be a way to support % for both modulus and percentages, I'm not yet sure though. If possible that would be ideal of course :)

@avianey
Copy link

avianey commented Jun 24, 2016

👍

@GoToLoop
Copy link

  • The modulo operator % is binary. That is, it demands 2 operands: (value)%(value)
  • Something like (value)+(value)% is awkward. It's binary as well, but finishes w/ %.
  • It also means that anything after % needs to be another operator, not a (value).
  • That is, the expression 100+25%%5 would be parsed as: 100*1.25%5.
  • Operator precedence would be tricky I believe.
  • 100%3+25%7%%5 perhaps would be parsed as: 1*1.04%5? Dunno... guess not.
  • The problem is not the postfix %, but the middle +. :-(

@josdejong
Copy link
Owner

You're right, it's not really doable, will give ambiguities in the expression parser which will lead to nasty situations.

I will close this issue now, feel free to reopen if anyone has a brilliant idea for a solution :)

@bornova
Copy link
Contributor

bornova commented Nov 20, 2018

I know this is an old topic but I wanted to share what I came up with for my application regarding percentages. If you use math.eval for your calculations as I do, this works pretty well. I am sure it's not perfect but check it out:
http://jsfiddle.net/rkmctvz6/2/

It treats % of as /100* and by using some regexes I was able to parse 100+20% as 100+(100*20/100) for example. It works with nested items in parentheses as well. If inputs are chained without parentheses, it solves from left to right although % of is given priority. The modulus operator % is also preserved. One thing it does not do is convert percentages to decimals such as 10% to 0.1.

I hope to see handling of percentages in Math.js one day!

@josdejong
Copy link
Owner

Thanks for sharing your solution @bornova!

@stoplion
Copy link

@bornova awesome solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants