See the calculator in TW-Scripts https://kookma.github.io/TW-Scripts/#A%20Calculator%20for%20Binary%20Operation
On Tuesday, October 22, 2019 at 8:59:41 AM UTC+3:30, Mohammad wrote: > > Hi A Gloom > > This is a variation of your simple macro. People with regex knowledge can > write simpler and better parser to use TW math operators for a wikitext > calculator. > > \define calc(ops, operation) > <$list filter="[<__input__>splitbefore[$ops$]removesuffix[$ops$]]" > variable=a> > <$list filter="[<__input__>removeprefix<a>removeprefix[$ops$]]" > variable=b> > <$list filter="[<a>$operation$<b>]" variable=result> > <<result>> > </$list> > </$list> > </$list> > \end > > \define calculator(input) > <$vars pattern="^\d+\s?[\+\-\*\/]\s?\d+$"> > <$list filter="[<__input__>regexp<pattern>]" variable=ignore > emptyMessage="Wrong input"><!--check input --> > <$list filter="[<__input__>search[+]]"> > <<calc ops:+ operation:add>> > </$list> > > <$list filter="[<__input__>search[-]]"> > <<calc ops:- operation:subtract>> > </$list> > > <$list filter="[<__input__>search[*]]"> > <<calc ops:* operation:multiply>> > </$list> > > <$list filter="[<__input__>search[/]]"> > <<calc ops:"/" operation:divide>> > </$list> > </$list> > </$vars> > \end > > !! Examples > > ``` > <<calculator 123+365>> > <<calculator 123-365>> > <<calculator 123*365>> > <<calculator 123/365>> > <<calculator 4+3>> > ``` > > will produce > > <<calculator 123+365>> > <<calculator 123-365>> > <<calculator 123*365>> > <<calculator 123/365>> > <<calculator -4>> > > > 488 > > -242 > > 44895 > > 0.336986301369863 > > Wrong input > > > > > On Tuesday, October 22, 2019 at 4:35:33 AM UTC+3:30, A Gloom wrote: >> >> \define ragscalculator2(cnumb1,copera,cnumb2) >> <$list filter=[[$cnumb1$]$copera$[$cnumb2$]]><<currentTiddler>></$list> >> \end >> >> <<ragscalculator2 (1st number) (math operator) (2nd number)>> >> >> >> ragscalculator2 : : macro name >> (1st number) : first number of your math equation >> (math operator) : the math operation you want preformed >> for + : use "add" >> for - : use "subtract" >> for x : use "multiply" >> for รท : use "divide" >> (2nd number) : second number of your math equation >> >> >> <<ragscalculator2 2 subtract 2>> >> >> >> produces just the solution to the equation/a number >> >> >> 0 >> >> >> Can be expanded for more numbers/equation steps and using the other math >> filter operators Tiddlywiki provides (abs, add, ceil, divide, exponential, >> fixed, floor, max, maxall, min, minall, multiply, negate, precision, >> product, remainder, round, sign, subtract, sum, trunc, untrunc) >> >> As with the calculator tiddler, the macro can be used with variables for >> input from other sources. >> >> >> <ragscalculator $(num1variable)$ $(operatorvariable)$ $(num2variable)$>> >> >> -- You received this message because you are subscribed to the Google Groups "TiddlyWiki" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/tiddlywiki/f492478b-9981-493e-8013-94b46141c9e1%40googlegroups.com.

