Currency Formatting Function

  • Share this:

Code introduction


This code defines a function that formats a currency value into a string representation with a specified currency and locale.


Technology Stack : The code uses the Babel library, specifically the Locale, dates, and numbers modules to format currency values.

Code Type : The type of code

Code Difficulty :


                
                    
import random
from babel import Locale, dates, numbers

def format_currency(amount, currency, locale='en_US'):
    """
    Formats a currency value to a string representation.

    :param amount: The amount to format.
    :param currency: The currency code to use.
    :param locale: The locale to use for formatting.
    :return: A string representation of the formatted currency.
    """
    formatted_currency = numbers.format_currency(amount, currency, locale=locale)
    return formatted_currency