Discussions

 View Only
  • 1.  Performing Multiplication in Jinja for Pipelines

    Posted 03-01-2023 18:11

    I am trying to set a field to

    [MyField] * .68

    I tried this:

    {{a.MyField  .68}} but that syntax wont even save. I also tried {{My.Field}}*.68. This saves, but produces an error when the pipeline runs. I know I could make a field in my Trigger table that does this calculation, but I would rather learn how to multiply in Jinja!



    ------------------------------
    Mike Tamoush
    ------------------------------


  • 2.  RE: Performing Multiplication in Jinja for Pipelines

    Posted 03-02-2023 13:14

    Assuming that [MyField] is a variable in your Jinja2 template that contains a numeric value, you can use the following expression to multiply it by 0.68:

    {{ MyField * 0.68 }}

    This expression multiplies the value of [MyField] by 0.68 and returns the result as a new value. The result will be a float value if [MyField] is a float, or an integer value if [MyField] is an integer.



    ------------------------------
    Shane Miller
    ------------------------------



  • 3.  RE: Performing Multiplication in Jinja for Pipelines

    Posted 03-02-2023 14:06

    I mistyped in my post. I should have said I tried {{MyField * .68}} and it would not work. 

    However, {{MyField * 0.68}} DOES work! Thank you! Turns out that leading zero is important.



    ------------------------------
    Mike Tamoush
    ------------------------------



  • 4.  RE: Performing Multiplication in Jinja for Pipelines

    Posted 03-02-2023 16:58
    Edited by Mike Tamoush 03-02-2023 17:07

    Any idea the syntax to round the number to 2 decimals?

    I tried {{MyField * 0.68 | round(2) }} but this did not work. 

    ------------------------------
    Mike Tamoush
    ------------------------------



  • 5.  RE: Performing Multiplication in Jinja for Pipelines
    Best Answer

    Posted 03-02-2023 17:13

    Round to 2 decimals: {{ "%.02f" | format(MyField * 0.68) }}



    ------------------------------
    Doug Henning
    ------------------------------



  • 6.  RE: Performing Multiplication in Jinja for Pipelines

    Posted 03-02-2023 17:22

    You're gonna laugh... well, or cry, depending on how long you've worked on this. go to the outcome fields properties. 

    Change this to 2 for 2 decimal places:



    ------------------------------
    Shane Miller
    ------------------------------



  • 7.  RE: Performing Multiplication in Jinja for Pipelines

    Posted 03-02-2023 17:24

    If you wanted to get fancy and do it in JINJA2 pipelines then plug this in:

    {{ (MyField * 0.68)|round(2) }}


    ------------------------------
    Shane Miller
    ------------------------------



  • 8.  RE: Performing Multiplication in Jinja for Pipelines

    Posted 03-02-2023 17:46

    Shane,

    Regarding the field properties, I did have that set, and so it works when viewing the record (field rounds to 2 decimals). However, if you are editing the record, the field still shows the initial input, which in my case was autofilled from the pipeline and had 4+ decimals. While maybe no one would notice, my OCD really wanted it filled with only 2 decimals :)

    Regrding your syntax, I agree that should work but for some reason, it is not working. round(2) in all documentation I googled seems to be what I would want, though it simply did nothing. However, Dougs solution did work.

    Happy to have a solution, but that solution is the least intuitive thing I have ever seen lol.

    Also, this entire time I could have simply made a field in my QB table, and had that field populate during my pipeline, but I really wanted to learn the proper Jinja code to do it without adding that field.



    ------------------------------
    Mike Tamoush
    ------------------------------



  • 9.  RE: Performing Multiplication in Jinja for Pipelines

    Posted 03-02-2023 13:26

    Just to elaborate, you would drag your [MyField] field from the pre-populated list on the right side into the field that you want the calculation to occur in. This will automatically format the field name for you by putting the appropriate step number a. or b. etc along with the {{ }} in the correct position



    ------------------------------
    Shane Miller
    ------------------------------