def format_currency(value, decimals=2):
    """
    Return a number suitably formatted for display as currency, with
    thousands separated by commas and up to two decimal points.

    >>> test = format_currency(1000)
    >>> test == '1,000'
    True
    >>> test = format_currency(100)
    >>> test == '100'
    True
    >>> test = format_currency(999.95)
    >>> test == '999.95'
    True
    >>> test = format_currency(99.95)
    >>> test == '99.95'
    True
    >>> test = format_currency(100000)
    >>> test == '100,000'
    True
    >>> test = format_currency(1000.00)
    >>> test == '1,000'
    True
    >>> test = format_currency(1000.41)
    >>> test == '1,000.41'
    True
    >>> test = format_currency(23.21, decimals=3)
    >>> test == '23.210'
    True
    >>> test = format_currency(1000, decimals=3)
    >>> test == '1,000'
    True
    >>> test = format_currency(123456789.123456789)
    >>> test == '123,456,789.12'
    True
    """
    number, decimal = (u'%%.%df' % decimals % value).split(u'.')
    parts = []
    while len(number) > 3:
        part, number = number[-3:], number[:-3]
        parts.append(part)
    parts.append(number)
    parts.reverse()
    if int(decimal) == 0:
        return u','.join(parts)
    else:
        return u','.join(parts) + u'.' + decimal
