def format(rep):
    """Formats the reputation score like it is formatted on the sites. Heavily based on CMS' JavaScript implementation at
        http://stackapps.com/questions/1012/how-to-format-reputation-numbers-similar-to-stack-exchange-sites/1019#1019"""
    str_rep = str(rep)
    if rep < 1000:
        return str_rep
    elif rep < 10000:
        return '%s,%s' % (str_rep[0], str_rep[1:])
    elif rep % 1000 == 0:
        return '%dk' % (rep / 1000.0)
    else:
        return '%.1fk' % (rep / 1000.0)
