Keep leading 0 in Twig

I think you must force the format (as your type is integer).

You can use the format filter :

{{ "%06d"|format(spending.documentName) }}

Or better create a twig extension (http://symfony.com/doc/current/cookbook/templating/twig_extension.html):

public function strpad($number, $pad_length, $pad_string) {
    return str_pad($number, $pad_length, $pad_string, STR_PAD_LEFT);
}

It's clearer in your template :

{{ spending.documentName | strpad(6,'0') }}

You need to use the format filter.

Since placeholders follows the sprintf() notation, you should be able to convert sprintf('%06d', $integer); in PHP to {{ '%06d'|format($integer) }} in Twig.


Kinda late here, but ran into the same.

With twig 3.x you can use:

{{your.number | format_number({min_integer_digit:'2'})}}

Tags:

Twig

Symfony