Regex Comma Number

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
@register.filter
def regex_comma_number(value,arg):
    import re
    

    __test__ = {}
    re_digits_nondigits = re.compile(r'\d+|\D+')
        
    parts = re_digits_nondigits.findall(arg % (value,))
    for i in xrange(len(parts)):
        s = parts[i]
        if s.isdigit():
            r = []
            for j, c in enumerate(reversed(s)):
                if j and (not (j % 3)):
                    r.insert(0, ',')
                r.insert(0, c)
            parts[i] = ''.join(r)
            break
    return ''.join(parts)

More like this

  1. PHP's number_format like template filter by janr 3 years, 10 months ago
  2. ImageURLField for forms by insin 4 years, 5 months ago
  3. format_thousands by cootetom 7 months, 2 weeks ago
  4. A RegexpField that clean the regex match using the desired format by nasp 1 year, 4 months ago
  5. Widget for CommaSeparatedIntegerField with pre-defined choices by rudyryk 1 year, 8 months ago

Comments

(Forgotten your password?)