Is it possible in Twig (used with Symfony2) to round number max to 8 decimal places OR less?
E.g.
number is 10.0001 so I would like to display it as 10.0001 (not 10.00010000)
number is 10.0000000000001 - display 10.0 (or 10)
number is 10.00100000001 -> display 10.001
Function number_format allows only to set static number of decimal places (e.g. always 8).
You might be able to get the result using number_format and trim like
{{ your_number | number_format(8) | trim (0) }}
That being said a custom twig extension like #bosam has recommended would probably be the easiest to use/remember in the future.
Best way for you to do this is a Custom Twig Extension implementing your logic.
That way you could use {{ myNumber | round_number }} and have the right rounded number to display.
Related
does anyone know if it's possible to divide two values?
For example:
product price / unit price = wanted result
Product price:
{{price.without_tax.formatted}}
A filter for unit price
{{#filter custom_fields 'Units per case' property='name'}}
<p>{{ value}}</p>
{{/filter}}
If I am to use something like:
{{#filter custom_fields 'Units per case' property='name'}}
<p>{{toFixed price.without_tax.formatted divide value}}</p>
{{/filter}}
I get a 0. If I don't include toFixed then nothing shows up. Not sure how to proceed. Please help.
(I know this is some crazy code but I don't know better.)
I'm seeing a number of issues. Try this:
{{#filter custom_fields 'Units per case' property='name'}}
<p>{{toFixed (divide ../price.without_tax.value value) 2}}</p>
{{/filter}}
Use price.without_tax.value instead of price.without_tax.formatted so it returns a number instead of a string.
Add ../ to the price object since it is nested inside {{filter}}.
The divide syntax is {{divide a b}}.
I added a parameter "2" to {{toFixed}} so it shows the result with 2 decimal places.
I'm trying to format the output of an expression as a percentage to be shown in a table. With a normal cell, appmaker lets you select the dropdown to #formatNumber. I can't figure out how to do this with a longer expression:
#datasource.item.ROI_Percent * (365/(#datasource.item.Sale_Date - #datasource.item.PO_Date))
I've tried throwing the whole thing in parens and adding #formatNumber but that doesn't seem to work. Is there another function I'm missing? I want this to be a rounded percentage (704%)
Thanks
Once you break away from a single bound output, App Maker can no longer determine the result type and thus doesn't allow you to use their helper functions. You'll have to use plain old javascript.
Here's one way to format as a percentage:
(#datasource.item.ROI_Percent * (365/(#datasource.item.Sale_Date - #datasource.item.PO_Date))).toLocaleString("en", {style: "percent"})
I have product name, which contains only one number.
Product.name="Pack of apples (12 pcs).";
Product.price=6;
What I'm trying to do - is to put 12 to variable, to do a little math later (ex: show how many costs 1 apple).
I was tried to do following:
{{ set apple = attribute(product, 'title'~lang)|replace('/[^0-9]/', '') }}
but with no luck.
I only have access to modifying .twig files, so any workaround would be appreciated
I would not reccommend using this way, however in this circumstance, the required data could be retrieved using simple split commands.
{% set bar = "Pack of apples (12 pcs)." %}
{% set foo = bar|split('(')[1]|split(' p')[0] %}
{{ foo }}
This would lead to foo containing 12.
However again, this would not be the best way to approach this and the use of an amount variable would be more approproate.
My goal is to mask one digit from a 4-digit string. Instead of having 2451, I'd like to have 24*1.
I tried {{ my_var|replace(slice(2, 1): '*') }} but this raises the following error: The function "slice" does not exist in My:Bundle:file.html.twig.
The weirdest thing being that {{ my_var|slice(2, 1) }} works perfectly. So the function does exists.
How can I do what I want to achieve?
Many thanks.
Create Your own Twig extension - filter:
SymfonyCookbook
IMHO it would be cleanest way to do this.
slice is a filter not a function, you can try to pipe them but in your case i do not see something achievable without creating your custom twig function or filter to mask your needs:
This is a question related to getting Drupal CCK fields (just in case that happens to change anything).
I have several Drupal CCK fields with similar names. They have the same name with a number at the end. that I'd like to pull values from these fields (ten fields total). This is the syntax for accessing the fields values:
$node->cck_field_1[0]['value']
$node->cck_field_2[0]['value']
$node->cck_field_3[0]['value']
…etc.
Since they're all separate fields, but they're numbered, I'd like to just loop through incrementally to write out what I need (there's a lot more to what I'm writing than just accessing these fields' data, but they're the determining factors of the rest), but I can't figure out how to insert a variable into that part of the code.
e.g., (if $i were the incremental number variable), I'd like to be able to write the following string as a variable:
'$node->cck_field_' . $i . '[0]["value"]'
I understand about using the curly brackets to create a variable name from a string, but the part I need the variable in needs to be outside of the string. e.g. this works:
${node}->cck_field_1[0]['value']
but this doesn't:
${node->cck_field_1}[0]['value']
(so I can't write ${'node->cck_field'.$i}[0]['value'] )
So how can write this so that I can use $i in place of the number?
This should work:
$node->{'cck_field_' . $i}[0]['value']