1 2 3 4 5 6 7 8 9 10 11 12 | @register.filter
def leading_zeros(value, desired_digits):
"""
Given an integer, returns a string representation, padded with [desired_digits] zeros.
"""
num_zeros = int(desired_digits) - len(str(value))
padded_value = []
while num_zeros >= 1:
padded_value.append("0")
num_zeros = num_zeros - 1
padded_value.append(str(value))
return "".join(padded_value)
|
More like this
- Template tag to create a list from one or more variables and/or literals by davidchambers 1 year, 4 months ago
- Filter to adjust forloop.counter across pages in a paginated view by egmanoj 2 years, 10 months ago
- Template range loop by nfg 2 years ago
- Duplicating Template Tag by skarphace 5 months ago
- RPN template math by durka 3 years, 3 months ago
Comments
hey, doesn't
do the same thing?
#
Probably. And I've also been informed that you can use the string formatting built-in template tag to accomplish this:
So, yeah -- good idea, but maybe not the best way to do it. I sort of suspected there was probably a better way. :)
#