htmlnode.jsswig-template

How to round off in swig template


I am using swig template to send auto mailer in my organisation. I have googled everything and couldn't find anything about how we can round off the value in swig. My code is:

<td style="background:#ff4d4d;text-align: center;padding: 7px 24px;border-radius: 5px;color: #fff;font-size: 18px;">{{cnt.p/cnt.total*100 }}%</td>

and in this I want to round off "{{cnt.p/cnt.total*100 }}" to upto 2 decimal places.Please help


Solution

  • Actually, you can use any JavaScript statement in {{}} in Swig. In this case, the code would be:

    <td>{{Math.round(cnt.p/cnt.total*10000)/100}}%</td>
    

    However, please note Swig is not maintained any more. Suggest using Mozilla's Nunjucks, which has syntax very similar to Swig.