Search code examples
nunjucksgulp-nunjucks-render

Render value to tenths place in nunjucks template?


Using the round() filter I can achieve the correct precision to the tenths place as in

{{ value | round(1) }}

however I still want to display the tenths place if value is zero or a whole integer. (0.0 instead of 0, 3.0 instead of 3)

Is there a different method or other way to render all values to the tenths place?


Solution

  • Here is the logic for the custom filter since the round filter will not maintain a tenths place for zero or whole integers:

    nunjucks.configure().addFilter('tenths', function(num) {
            return parseFloat(num).toFixed(1);
        });
    

    Then usage is the same as any filter:

    {{ num | tenths }}