Currency Formatting Function

  • Share this:

Code introduction


This function takes an amount, a currency code, and a locale code as arguments, and uses the format_currency function from the Babel library to format the amount into a string representing the specified currency and locale.


Technology Stack : Babel

Code Type : Function

Code Difficulty : Intermediate


                
                    
import random
from babel.numbers import format_currency

def generate_currency_format(amount, currency, locale='en_US'):
    """
    Formats the given amount into a currency string.

    :param amount: The amount to format.
    :param currency: The currency code.
    :param locale: The locale to use for formatting.
    :return: The formatted currency string.
    """
    formatted_amount = format_currency(amount, currency, locale=locale)
    return formatted_amount                
              
Tags: