Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2020-09-05 13:27:20

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

Detect if variable value is negative

I have a Price custom field. I want to perform a calculation on it and work out if the result is negative. That then affects what I do next.

<!-- 10% deposit as standard, rounded to 2DP...ish -->
<txp:variable name="deposit" escape="trim">
   <txp:evaluate query='round(((<txp:custom_field name="Price" /> div 10) * 100)) div 100' />
</txp:variable>

<!-- Work out any excess price over 5K -->
<txp:variable name="depositExtra" escape="trim">
   <txp:evaluate query='<txp:custom_field name="Price" /> - 5000' />
</txp:variable>

<!-- Recalculate deposit if price greater than 5K -->
<txp:if_variable name="depositExtra" match="pattern" not value="^\-[\d]+$">
OVER 5K
</txp:if_variable>

I see OVER 5K regardless of custom field Price input value, whether it’s 4495 or 5595, for example.

Is my regex wrong? It seems to work fine on the online regex testers. I’ve tried the value with and without forward slashes, with and without start and end markers. With and without escaping the minus sign. No deal.

What am I missing? I thought if_variable pattern matching worked like that? Any better way I could approach it?

It works this way:

<txp:evaluate query='<txp:variable name="depositExtra" /> > 0'>
OVER 5K
</txp:evaluate>

so I’ve done that for now, I just wondered why I couldn’t get the if_variable route to work.

EDIT: Sub-question: if the value ends in a zero, it gets truncated to something like 249.5 when I really want it to be 249.50. Any ideas on how to do that?

Last edited by Bloke (2020-09-05 14:18:04)


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

#2 2020-09-06 07:45:36

colak
Admin
From: Cyprus
Registered: 2004-11-20
Posts: 8,269
Website

Re: Detect if variable value is negative

Did you work this one out?


Yiannis
——————————
neme.org | hblack.net | LABS | State Machines | NeMe @ github | Covid-19; a resource
I do my best editing after I click on the submit button.

Offline

#3 2020-09-06 11:31:26

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

Re: Detect if variable value is negative

Not yet. Just went with the evaluate solution for now. But it’d be nice to figure out where I’m going wrong with the if_variable approach.


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

#4 2020-09-06 14:34:07

etc
Developer
Registered: 2010-11-11
Posts: 3,863
Website

Re: Detect if variable value is negative

Bloke wrote #325793:

What am I missing? I thought if_variable pattern matching worked like that? Any better way I could approach it?

Your code works fine for me.. you are on 4.8.2+, right? Could it be textile interfering?

Sub-question: if the value ends in a zero, it gets truncated to something like 249.5 when I really want it to be 249.50. Any ideas on how to do that?

You could register php number_format() function and use it in <txp:evaluate />.

Offline

#5 2020-09-06 14:42:17

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

Re: Detect if variable value is negative

etc wrote #325816:

Your code works fine for me.. you are on 4.8.2+, right?

What’s the embarassed emoji again? 4.7.3. Damit, that explains everything and of course, live site, didn’t get any warnings. Doofus. Thank you. Thought I was going mad!

You could register php number_format()

Aye, if there’s nothing native, that would work, 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

#6 2020-09-06 14:57:52

etc
Developer
Registered: 2010-11-11
Posts: 3,863
Website

Re: Detect if variable value is negative

Bloke wrote #325818:

Aye, if there’s nothing native, that would work, thanks.

Multiplying by 100, rounding and trim-replacing the last (\d\d)$ with .$1 should work too.

Offline

Board footer

Powered by FluxBB