Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#21 2010-07-19 08:59:15

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 9,647
Website

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,254
Website

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,254
Website

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: 389

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

#25 2013-01-23 09:23:02

gomedia
Plugin Author
Registered: 2008-06-01
Posts: 1,254
Website

Re: adi_calc – Calculator for TXP variables

Hi Jorge,

milosevic wrote:

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’s what pops out of the standard PHP function round().

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.

I might have to think about that. In the meantime would something like this help?

<txp:rah_function call="number_format" number="34.5" decimals="4" dec_point="," thousands=" " />

You’d have to sort out feeding the value in & getting the result into your TXP variable, but the above chucks out 34,5000. Is that what you’re after? How do the Spanish handle thousands? I guessed at a space character.

Alternatively, you could delve into PHP to manipulate the variable directly (not tested):

<txp:php>
globals $variable;
$variable['myvar'] = number_format($variable['myvar'],4,',',' ');
</txp:php>

Offline

#26 2013-01-23 10:13:22

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 9,647
Website

Re: adi_calc – Calculator for TXP variables

gomedia wrote:

How do the Spanish handle thousands? I guessed at a space character.

Take a look at the last few lines of format_filesize() in txplib_misc.php. That uses localeconv to take some of the guesswork out of it and set language-specific defaults in number_format().

Last edited by Bloke (2013-01-23 10:14:30)


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

#27 2013-01-23 12:04:22

gomedia
Plugin Author
Registered: 2008-06-01
Posts: 1,254
Website

Re: adi_calc – Calculator for TXP variables

Bloke wrote:

Take a look at the last few lines of format_filesize() in txplib_misc.php

Ta very much!

Offline

#28 2013-01-23 18:01:31

milosevic
Member
From: Madrid, Spain
Registered: 2005-09-19
Posts: 389

Re: adi_calc – Calculator for TXP variables

gomedia dijo:

I might have to think about that. In the meantime would something like this help?

Thanks. I’ll give it a try.

How do the Spanish handle thousands? I guessed at a space character.

thousands = dot
floating point = comma

Last edited by milosevic (2013-01-23 18:03:21)


<txp:rocks/>

Offline

#29 2013-12-03 13:18:37

springworks
Member
Registered: 2005-01-06
Posts: 172
Website

Re: adi_calc – Calculator for TXP variables

I’ve tweaked the code of adi_calc to allow you to specify the value of an existing Textpattern variable as the value for the reset counter. This makes it really useful to reset counters based on dynamic results.

For example, I’m using it to fetch a batch of articles using smd_query, then to split the results into three columns by detecting when the current row reaches a calculated break value.

Here’s a snippet of the code I’m using for that:

<txp:variable name="counter" value="0" />
<txp:variable name="reset_counter" value="0" />

<div class="first column">

<txp:smd_query column="*" table="textpattern" where="section='articles' AND status='4' ORDER BY position asc" populate="article">
    <txp:adi_calc name="total-results" value="{smd_allrows}" />
    <txp:adi_calc name="total-results" div="3" ceiling="1" result="break-value" />
    <txp:adi_calc name="counter" add="1" reset="break-value" reset_name="reset_counter" />

    … Output data for current article here …

<txp:if_variable name="counter" value="0">
  <txp:if_variable name="reset_counter" value="1">
	</div>
	<div class="second column">
  </txp:if_variable>
  <txp:if_variable name="reset_counter" value="2">
	</div>
	<div class="third column">
  </txp:if_variable>
</txp:if_variable>

</txp:smd_query>

</div>

And here’s the extra code I added to adi_calc just before line 74:

  // check 'reset' attribute for name of existing TXP variable and set value if it exists
  if (array_key_exists($reset,$GLOBALS['variable'])) // use TXP variable value from 'reset' attribute
    $reset = $GLOBALS['variable'][$reset];

Just in case you wanted to add this to the next version!

Offline

#30 2013-12-03 21:59:52

gomedia
Plugin Author
Registered: 2008-06-01
Posts: 1,254
Website

Re: adi_calc – Calculator for TXP variables

springworks wrote:

I’ve tweaked the code of adi_calc to allow you to specify the value of an existing Textpattern variable as the value for the reset counter.

Hi Steve, thanks for that. Perhaps I should make all the attributes flexible, so they can accept a numerical value or a variable?

I’m all in favour of brevity:

reset="break-value"

is more instantly comprehensible than:

reset='<txp:variable name="break-value" />'

I’ll bear it in mind …

Offline

Board footer

Powered by FluxBB