Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

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

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

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: 11,445
Website GitHub

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,373

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

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,373

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

#31 2015-06-15 01:13:20

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

Re: adi_calc – Calculator for TXP variables

Version 1.2 available for download – nothing exciting, just TXP 4.6 tag registration.

Offline

#32 2021-04-28 01:58:13

peterj
Member
From: Melbourne, Australia
Registered: 2005-06-02
Posts: 99

Re: adi_calc – Calculator for TXP variables

Still love this little mathematical swiss army knife. Just updating an old site to TXP 4.8.5. It already had adi_calc v1.2, but is showing the “tag not registered” warning again. Fixed by changing this:

if (class_exists('Textpattern_Tag_Registry')) {
	Txp::get('Textpattern_Tag_Registry')
		->register('adi_calc')
	;
}

To this:

if (class_exists('\Textpattern\Tag\Registry')) {
	Txp::get('\Textpattern\Tag\Registry')
		->register('adi_calc')
	;
}

Offline

#33 2021-04-28 02:41:46

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

Re: adi_calc – Calculator for TXP variables

peterj wrote #329981:

Still love this little mathematical swiss army knife.

An oldie … but a goodie.

… showing the “tag not registered” warning again.

The latter (correct) code is what’s present in adi_calc v1.2 available for download. I do remember there was a last minute change in the tag registration format after I’d updated & uploaded a whole load of my plugins. I wonder if you originally downloaded a copy before I’d snuck in a secret update.

Last edited by gomedia (2021-04-28 02:41:58)

Offline

#34 2024-03-16 18:20:17

robhert
Member
From: Perú
Registered: 2007-04-27
Posts: 206
Website

Re: adi_calc – Calculator for TXP variables

Hello!, back around here. I would like to know if this plugin is still active or if there is another alternative that allows the same thing. In my case, I require the numbering to increase in <div data-number=“1”>

Offline

#35 2024-03-16 20:25:36

etc
Developer
Registered: 2010-11-11
Posts: 5,186
Website GitHub

Re: adi_calc – Calculator for TXP variables

Welcome back, long time no see. Depending on the context, you can try

  • either <txp:variable name="counter" add output />
  • or (inside an article/file/image/link list) <txp:yield item="count" />

Offline

#36 2024-03-16 23:09:26

robhert
Member
From: Perú
Registered: 2007-04-27
Posts: 206
Website

Re: adi_calc – Calculator for TXP variables

It’s working! Thank you very much.

Offline

Board footer

Powered by FluxBB