Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
Re: adi_calc – Calculator for TXP variables
This is a wonderful plugin, and I think it would be a very useful feature to allow rounding up to the next whole number with remainders from division operations. Any way you could add this feature, Adi? I know I can use smd_if, but it would be nice to not have to install that for this single purpose. Or is there a way to do this as is?
Offline
Re: adi_calc – Calculator for TXP variables
+1 for the rounding option.
And, while here, I would like to ask for another feature: to make adi_calc
perform basic math operations on other stuff that may not be a txp:variable
.
Suppose we are using some of those crazy smd_ plugins that include some {replacement}
tags.
<txp:adi_calc value="{width}" add="5" display="1" />
Or maybe, easier/better, to have a container mode to use it:
<txp:adi_calc add="5" display="1">{width}</txp:adi_calc>
I think it could be useful to have a way to do some basic math operations without having to do them inside txp:php
.
Offline
#15 2010-05-18 01:35:57
- gomedia
- Plugin Author
- Registered: 2008-06-01
- Posts: 1,373
Re: adi_calc – Calculator for TXP variables
aswihart wrote:
… a very useful feature to allow rounding up to the next whole number …
Sounds useful, I’ll look into it.
maniqui wrote:
… make adi_calc perform basic math operations on … some of those crazy … {replacement} tags.
Hi Julián. Now that’s an interesting suggestion. I’ll have a ponder on that too.
Offline
Re: adi_calc – Calculator for TXP variables
Sorry, for offtopic.
maniqui wrote:
+1 for the rounding option.
<txp:adi_calc add=“5” display=“1”>{width}</txp:adi_calc>
<txp:aks_evar calc="/3" round="2">77</txp:aks_evar>
Other samples
Last edited by makss (2010-05-18 03:35:33)
aks_cron : Cron inside Textpattern | aks_article : extended article_custom tag
aks_cache : cache for TxP | aks_dragdrop : Drag&Drop categories (article, link, image, file)
Offline
#17 2010-05-18 22:25:06
- gomedia
- Plugin Author
- Registered: 2008-06-01
- Posts: 1,373
Re: adi_calc – Calculator for TXP variables
makss wrote:
<txp:aks_evar calc="/3" round="2">77</txp:aks_evar>
It’s an arms race!
Offline
#18 2010-05-24 10:38:44
- gomedia
- Plugin Author
- Registered: 2008-06-01
- Posts: 1,373
Re: adi_calc – Calculator for TXP variables
Version 0.3 now available for download:
- fixed: error if adi_calc is the first person to set a TXP variable (thanks maniqui)
- new attribute: ‘ceiling’ to always round up the result of integer division (for aswihart)
Offline
Re: adi_calc – Calculator for TXP variables
Thank you Adi, this update is working perfectly.
Offline
#20 2010-07-19 00:23:46
- gomedia
- Plugin Author
- Registered: 2008-06-01
- Posts: 1,373
Re: adi_calc – Calculator for TXP variables
adi_calc bumped up to version 1.0 which is now available
New attributes:
- ‘precision’ to switch into floating point mode
- ‘result’ variable set to result of calculation, original variable left unchanged
- ‘reset’ to automatically reset value to zero
- ‘reset_name’ to count the number of resets
Enhancements:
- container tag mode
- no longer complains if an initial value not found – if all else fails, default to zero
- can operate without TXP variables entirely
Offline
Re: adi_calc – Calculator for TXP variables
Nice enhancememts Adi, thanks — and almost exactly what I need.
Why almost? Because I’d like to round (ceiling
?) or — probably more accurately — use precision="0"
on multiplication to only return an integer in this case. I’m multiplying by a fraction like 0.0123456
so I get a fractional result that I’d like to lop off. Currently I add a line of PHP beneath adi_calc to do a round()
or a ceil()
on the $variable
it creates (or I suppose I could add a line of PHP before it and set the variable to 1/my_fraction
and then use the div
operator). But it would be awesome if I could do it without. Is that possible?
btw, result
is incredibly useful because I’m using the same variable in a few calculations so, previously, I had to copy it to a bunch of other variables ahead of the calculations. But now I don’t have to. Yay, thanks!
The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.
Txp Builders – finely-crafted code, design and Txp
Offline
#22 2010-07-20 02:26:38
- gomedia
- Plugin Author
- Registered: 2008-06-01
- Posts: 1,373
Re: adi_calc – Calculator for TXP variables
Bloke wrote:
Nice enhancememts Adi, thanks — and almost exactly what I need.
Having looked back at the code I can see that adi_calc is a little inconsistent when it comes to integer & floating point modes. I’m working on an update to clean it all up.
Offline
#23 2010-07-21 00:37:15
- gomedia
- Plugin Author
- Registered: 2008-06-01
- Posts: 1,373
Re: adi_calc – Calculator for TXP variables
Version 1.1 now available.
The default mode of operation is now integer – i.e. regardless of what values you supply, integer results will be produced. It’s worth mentioning here that in integer mode division will always round down.
It you want to produce floating point results use the precision attribute: precision=""
to simply switch it on, precision="x"
to round up/down to x decimal places.
If you’re upgrading from a pre-1.0 version and you’ve been using floating point calculations then you now need to use the precision
attribute.
Thanks to Bloke for discovering a hidden feature. adi_calc can be used to round numbers without having to do any calculations. For example:
<txp:variable name="pi" value="3.14159265" />
<txp:adi_calc name="pi" precision="2" />
will round the variable pi to 2 decimal places.
Offline
#24 2013-01-23 06:46:13
- milosevic
- Member
- From: Madrid, Spain
- Registered: 2005-09-19
- Posts: 390
Re: adi_calc – Calculator for TXP variables
Hi Adi.
I found a little problem using the precision atribute.
If I set precision to 4 but the calculation result is, for example, 34.5000 adi_calc returns 34.5 (without the final zeroes).
That behavior its a problem in many circumstances, for instance for showing aligned sets of numbers and for manipulating the calculation result as text in order to force the decimal and thousand characters presentation in a determined languague format (ej: spanish 34,5 vs english 34.5)
This is not a bug, but perhaps a new param such “zerofill”=“1/0” will be very usefull to let user to control the result presentation filling or not filling the floating point part of calculation with zeroes to keep or not the number of digits as set in the precision param.
<txp:rocks/>
Offline