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.
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.
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.
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 :)
:+1:
% is binary. That is, it demands 2 operands: (value)%(value)(value)+(value)% is awkward. It's binary as well, but finishes w/ %.% needs to be another operator, not a (value).100+25%%5 would be parsed as: 100*1.25%5.100%3+25%7%%5 perhaps would be parsed as: 1*1.04%5? Dunno... guess not.%, but the middle +. :-(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 :)
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!
Thanks for sharing your solution @bornova!
@bornova awesome solution.
Most helpful comment
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
% ofas/100*and by using some regexes I was able to parse100+20%as100+(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% ofis 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!