How to specify decimals when rounding the result of an operation using math.js Plugin?

Hello,
I am using the “<$calc>” widget for operations that I can’t make with the usual mathematics operators. And I would like the result to appear rounded to two decimals but I can’t find how.
For exemple :

<$calc>fix(7.12333)</$calc> displays 7 but I want it to show 7,12.

How can I specify the number of decimals? The Math.js way doesn’t seem to be working here (“Unable to parse”).

A workaround may also work for me.
I understand this may be a beginner’s question, but any clue would still be appreciated.
Thanks in advance !

Welcome Luna,

Are you sure you can’t use the other filter operators now, they have being extended regularly?

Can you share the maths you are doing.

Never the less however if using the $calc widget or even evans formula plugin I belive the trick is to set a variable to the result of your calculation. You can then make use of the filter operators such as “fixed”.

The trick is to make use of the wikify widget (just in time) to ensure “pre-evaluation” before use in the filter; here is an untested example. It is my convention to reuse the macro variable name, but you can have a different one if you desire;

\define calc-value() use your $calc widget here to give the result you are after
<$wikify name=calc-value text="""<<calc-value>>""">
in here "<<calc-value>>" actually contains the value not the wikitext of the macro
eg {{{ [<calc-value>fixed[2]] }}}
</$wikify>

I have not tested this but you could quite easily.

2 Likes

Thanks a lot ! It works.
In fact, I had already tried the variable method but I always had “0.00” as a result. The “wikify” widget is the brick I’ve been missing.
Because it displayed the result as a missing link, I just had to add

\define tv-show-missing-links() no

at the beginning, and everything’s fine !

1 Like

I have found that math.js have the round function and that this is working:

A = <$calc>6000 / (400 * 0.87)</$calc> = 17.24137931034483

Around = <$calc>round( 6000 / (400 * 0.87) , 2)</$calc> = 17.24