-
Notifications
You must be signed in to change notification settings - Fork 37
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
Support percentage operator in parser #53
Comments
Well I have had to do my method and every time I press the button with the symbol "%", "/ (100) *" is added, then I apply a format so that "/ (100) *" becomes "% "but only in sight. |
Good to hear that you found a workaround. Will consider adding support for this to the parser! |
Any news about adding the percent operator? |
It seems that nothing yet, it has been a long time since I gave my option, that although it is not the best it is how I fix it, if it helps you let me know and I put my code although it is more or less explained above and if not I hope the creator is current early. |
is any update for this issue ? |
Here is my workaround (the key here is the regex with the extension Expression on String {
double evaluate(Map<String, double> variables) {
final cm = ContextModel();
for (final variable in variables.entries) {
cm.bindVariableName(variable.key, Number(variable.value));
}
final percentsReplaced = replaceAllMapped(
RegExp(r'(\d+(\.\d*)?)%'),
(match) => '(${match.group(1)!}/100)',
);
return Parser().parse(percentsReplaced).evaluate(EvaluationType.REAL, cm);
}
} However, this will break support for modulo unless you have a space before the @fkleon I was taking a look at the factorial support PR (#58) to see how feasible it would be for me to make a PR for basic percentage support (supporting the equivalent complexity of |
HI @GregoryConrad - you're right about the shortcomings of the current lexer. I've looked into alternative implementations but never finished them. Busy elsewhere so struggling to find any time to work on this library!
Coming from a programming background I find the modulo operator to be a more "natural" fit for |
@fkleon In the end, as I commented some time ago in a comment, my solution was to divide by 100, to get that decimal result and be able to do the same thing I want (for example: 25/100 => 0.25), and then simply when you hit the operator % replacement 25/100 for the same (25/100 => 25%). I know it's a mess, but if you look at all calculators have this and that's why I said, since they are for everyone there are people who just don't want to think about how to do it. |
It just doesn't work right, I have tried to do the calculation of various percentages but it doesn't work right, it gives wrong result.
For example if I put something as simple as:
'8%9'.interpret();
the output is: 8
when it should be: 0.72
If you could fix it please because this gives me a lot of problems and I need to make a calculator that is already ready and that error will not let me finish it.
The text was updated successfully, but these errors were encountered: