Problems, need help? Have a tip or advice? Post it here.
10 posts Page 1 of 1
Hi,

I'm just starting with couch and can't seem to get a solution for a calculation.

The site has a product listing pages on the admin side the user has to fill the buying price of the product and the markup % the company want to add on the product on the front-end (product page) the result should be a visible price tag of the buying price plus the markup %.

I have a simple calculation for this on a Twig based site but I can't seem to get there with the variables in couch... the Twig solution is :

{% set percent = ( record.markup ) %}
{% set temppercent = ( 100 + percent ) %}
{% set total = ( record.buyprice ) %}
{% set final = ( total / 100 * temppercent ) %}

The final out-put = buying price + the markup percentage.

Can someone point me to a solution to get this done with couch?

Thanks
I load frameworks and write bugs on top of them, after that I rearrange the code so that it looks like a cool product.
Let's assume you have two editable regions: one is named 'price' for the buying price, and another is named 'markup' for the markup percentage. Both code examples below do the same thing, however one is obviously more readable than the other:
Code: Select all
<cms:set markup_pct = "<cms:add markup '100'/>"/>
<cms:set markup_pct = "<cms:div markup_pct '100'/>"/>
<cms:set total_price = "<cms:mul price markup_pct/>"/>
Code: Select all
<cms:set total_price = "<cms:mul price "<cms:div "<cms:add markup '100'/>" '100'/>"/>"/>

You should read http://www.couchcms.com/docs/concepts/setting-parameters.html if you have not already as it explains the single/double/no quote usage. Depending on how you wish to display the total price you will want to use the 'number_format' tag (http://www.couchcms.com/docs/tags-reference/number_format.html):
Code: Select all
<cms:number_format total_price/>
Thanks, works nicely !

Yes, I did read the documentation but couldn't figure this out. Thanks again for this snippet.
I load frameworks and write bugs on top of them, after that I rearrange the code so that it looks like a cool product.
On the same logic / principal - how would one go about doing a simple conversion of miles to kilometres? Where 1 mile = 1.60934km? The same principal could then be applied to things like currency where it might be possible to use XE.com feed / API / Ajax

I toyed with the example, adjusting as I thought might work but no dice
If we already have the conversion rate from one unit to another, whether it be for distance, weight, temperature, currency, etc... the process is much the same. So for example, to convert 25 miles to kilometers, mathematically we would do:
25 miles × 1.60934 km/mile = 40.2335 km
In Couch we would use the 'mul' (multiply) tag and just plug in two parameters which in this case are the operands:
Code: Select all
<cms:mul '25' '1.60934' />
If we have any of these in variables:
Code: Select all
<cms:set distance = '25' />
<cms:mul distance '1.60934' />

OR

<cms:set distance = '25' />
<cms:set KM_PER_MILE = '1.60934' />
<cms:mul distance KM_PER_MILE />

If you have a specific requirement to convert currencies using live exchange rates, I'm sure we can work something out.
Sorry Cheesypoof, thought I had posted this last night but i must have had the "Cant post so soon message"

ignore my previous - had a microscopic brainwave / picture of reason and tried this and it works fine - is it just me or are there limited docs on "math" in couch? I did try a couple of searches, there could be a section on this as it would be great and adds a lot of flexability on site building for things like currency etc.

Code: Select all
<cms:mul "<cms:show milage />" '1.60934' />


the above could be fancy / jazzed up where you dynamically set "1.60934" with a figure for currency applications.
Sorry, forgot to ask - on the note of using this:

Code: Select all
<cms:mul "<cms:show vf_milage />" '1.60934' />


Is there a simple means to ROUND to say 2 figures or 0 decimal places, and how to display the output in a comma format i.e.

56,250 KM
There isn't an in-depth core concepts page for the math tags, but all of them are documented sufficiently in the tags reference. See the related tags at the bottom of docs/tags-reference/mul.html.

You don't need to use the 'show' tag by the way; it is redundant in this case because parameters with no quotes are treated as variables:
Code: Select all
<cms:mul vf_milage '1.60934' />

You will want to use 'the number_format' tag (docs/tags-reference/number_format.html) to display the number:
Code: Select all
<cms:number_format "<cms:mul vf_milage '1.60934' />" decimal_precision='0' />
@cheesypoof

Solid! Thanks for your pointers! Much appreciated :)
Hi,
i have two editable regions and one repeatable regions
Code: Select all
<cms:editable name='price' type='text'/>
<cms:editable name='price1' type='text'/>

<cms:repeatable name='test' > 
<cms:editable name='amount' type='text'/>
</cms:repeatable>

and I apply
Code: Select all
<cms:set total = "<cms:add price price1/>"/>
<cms:number_format total/>

its working fine, but how can I configure repeatable "amount" on its
I tried
Code: Select all
<cms:set total = "<cms:add amount price price1/>"/>
<cms:number_format total/>

but it showing Warning: A non-numeric value encountered in C:\xampp\htdocs\cashflow\couch\tags.php on line 1231

Kindly help me

Thanks
10 posts Page 1 of 1