def format_currency(amount, convert_to_dollars=True):
    """
    Pad currency with 2 decimals and commas,
    optionally convert cents to dollars.

    :param amount: Amount in cents or dollars
    :type amount: int or float
    :param convert_to_dollars: Convert cents to dollars
    :type convert_to_dollars: bool
    :return: str
    """
    if convert_to_dollars:
        amount = cents_to_dollars(amount)

    return '{:,.2f}'.format(amount)
    def serialize(self):
        """
        Return JSON fields to render the coupon code status.

        :return: dict
        """
        params = {"duration": self.duration, "duration_in_months": self.duration_in_months}

        if self.amount_off:
            params["amount_off"] = cents_to_dollars(self.amount_off)

        if self.percent_off:
            params["percent_off"] = (self.percent_off,)

        return params
Exemplo n.º 3
0
    def serialize(self):
        """
        Return JSON fields to render the coupon code status.

        :return: dict
        """
        params = {
            'duration': self.duration,
            'duration_in_months': self.duration_in_months,
        }

        if self.amount_off:
            params['amount_off'] = cents_to_dollars(self.amount_off)

        if self.percent_off:
            params['percent_off'] = self.percent_off,

        return params
 def test_cents_convert_to_dollars(self):
     """ Cents become dollars. """
     assert cents_to_dollars(0) == 0.0
     assert cents_to_dollars(5) == 0.05
     assert cents_to_dollars(-20) == -0.2
     assert cents_to_dollars(100) == 1