0

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?

4

2 回答 2

0

添加您自己的过滤器

var env = nunjucks.configure(...

env.addFilter('round1', function(num) {
    if (!isNaN(num))
        return '???';

    return parseFloat(num).toFixed(1);    
});

用法

{{ value | round1 }}
于 2017-05-23T11:44:12.597 回答
0

这是自定义过滤器的逻辑,因为圆形过滤器不会为零或整数保持十分位:

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

然后用法与任何过滤器相同:

{{ num | tenths }}
于 2017-05-23T23:47:36.433 回答