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?

Don
  • 3,876
  • 10
  • 47
  • 76

2 Answers2

0

Add you own filter

var env = nunjucks.configure(...

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

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

Usage

{{ value | round1 }}
Aikon Mogwai
  • 4,954
  • 2
  • 18
  • 31
  • The above logic will return '???' for everything because the if statement is not enclosed, and the logic is reversed for isNaN. – Don May 23 '17 at 23:49
  • 1
    Yeah. It's a bug. `if (isNaN(num)) ...` is correct check, of course. – Aikon Mogwai May 24 '17 at 00:03
0

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 }}
Don
  • 3,876
  • 10
  • 47
  • 76