How can I multiple two decimal values in twig ?
Let's say var1 = "37.57" and var2 = "8" and I tried with
{{ var1 * var2 }}
But it multiplies only the integer value while ignoring the decimal it returns "296" which is 37 * 8.
Can you try with
{{ var1|number_format * var2|number_format }}
Maybe it doesn't work because of string format (even if string format should work).
Doc for number_format
The integer result may be produced if wrong decimal separator is used.
Okay I got it working.
For some reason the twig is considering it as a "string" instead of "interger" not sure why it is, since i simple return the data that i receive through external API connection.
here is the thing which worked for me:
{% set var_1 = 0 + var1|numberformat(2,'.',',') %}
{% set var_2 = 0 + var2|numberformat(2,'.',',') %}
then,
{{ (var_1 * var_2)|numberformat(2,'.',',')}}
Related
I want to make an excerpt of an entity resume.
So I do
{{ randBook.resume|slice(0, 200) ~ '...'}}
It work but i'm french and all of my spécial caracteres are like this #%00 so how I want to convert into UTF-8 ?
And I Try it
{{ randBook.resume|striptags|convert_encoding('UTF-8', 'ISO-8859-1')|slice(0, 200) ~ '...'}}
But it's the same.
Thanks for your help
I would like to format the numbers being displayed in a "Computed Twig" element in Drupal 8 Webform. I've tried Twig's "number_format" filter, but it does not work. I have this:
{{ (data.auswahl_oder_eingabe) * 202 / 1000000 * 25|number_format(2, ',', '.') }}
I would like to have only two decimals, the dot as a thousands separator and the comma as a decimal point. But I always get something like this:
53.025
So three decimals and the dot as a decimal separator. The format I would like to have in this case would be: 53,03 No matter what I write in that "number_format" definition, the result doesn't change. Does this filter work at all in Drupal 8.7.7? Or is there a mistake I made somewhere in the markup?
I believe that your filter is only applying to the 25 not the entire calculated value. Try something like this:
{% set calculated = data.auswahl_oder_eingabe * 202 / 1000000 * 25 %}
{{ calculated|number_format(2, ',', '.') }}
I am displaying a list of datetimes from my database and using twig to format. This was working until today.
If I dump this: {{ dump(time.clockedIn) }}
I get this:
DateTime {#612 ▼
+"date": "2017-02-03 17:54:20.000000"
+"timezone_type": 3
+"timezone": "America/New_York"
}
When I dump this: {{ dump(time.clockedIn|date("m/d/Y h:m:s a")) }}
I get this: "02/03/2017 05:02:20 pm"
I cannot find out why this changed. Any ideas?
Try this instead:
{{ dump(time.clockedIn|date("m/d/Y g:i:s a")) }
As per the Twig date documentation, you should be using g:i:s a.
Note that this documentation references PHP's date function, where you'll see the proper format characters that you can use.
In my Twig project I use translation from .yaml files. The text which I need to translate is found in a .html.twig file and says, for example,
"My text for translation contains a {{parameter}} to translate."
I know that I can replace this whole string with a key word, for example - to_translate %parameter% to_translate2, and I can use the translation from my .yaml file like this
to_translate: "My text for translation contains a"
to_translate2: "to translate"
And the parameter will be passed. However, how can I do this without breaking the sentence in so many parts?
Maybe I didn't get your point, but you can add as many parameters as you want ?
Yaml file:
my_translation_key: Hello %firstname%, %lastname%, welcome here !
In Twig:
{{ 'my_translation_key' | trans({
'%firstname%': 'John',
'%lastname%': 'Doe'
}) }}
If what you want is to get nested blocks in your translations, you can probably try something like this:
Yaml file:
my_translation_key: Hello %firstname%, %lastname%, %welcome% !
welcome_block: welcome %where%
In Twig:
{% set welcome = 'welcome_block' | trans({'%where%': 'here'}) %}
{{ 'my_translation_key' | trans({
'%firstname%': 'John',
'%lastname%': 'Doe',
'%welcome%': welcome
}) }}
Maybe you should try this :
{{ ('My text for translation contains a '~parameter)|trans }}
In Documentation :
~: Converts all operands into strings and concatenates them. {{ "Hello " ~ name > ~ "!" }} would return (assuming name is 'John') Hello John!.
I have the following two variables in twig file.
{%set sanstha_target%}
{%render 'FrontBundle:Datasheet:getCurrentTarget' with {'dealer':recent_entry.id,'month':recent_entry.year~recent_entry.month,'type':'3'}%}
{%endset%}
{%set sanstha_actual%}
{%render 'FrontBundle:Datasheet:getTotal' with {'datasheet':recent_entry.datasheet,'dealer':recent_entry.id,'type':'3'}%}
{%endset%}
The when I am going to divide this getting this error:
An exception has been thrown during the rendering of a template ("Notice: Object of class Twig_Markup could not be converted to int...
I need to get the percentage like this
{{ (sanstha_actual/sanstha_target)*100}}
I think you have to use two slashes, i.e.
{{ (sanstha_actual // sanstha_target) * 100 }}
as shown in the documentation:
//: Divides two numbers and returns the floored integer result. {{ 20
// 7 }} is 2