Random Currency Converter Using Babel

  • Share this:

Code introduction


This function uses Babel's format_currency function to convert an amount from one currency to another, and returns the formatted representations of both the original and converted amounts.


Technology Stack : Babel

Code Type : Function

Code Difficulty : Intermediate


                
                    
import random
from babel.dates import format_date
from babel.numbers import format_currency

def random_currency_converter(amount, from_currency, to_currency):
    # This function converts an amount from one currency to another using Babel's format_currency
    try:
        formatted_amount = format_currency(amount, from_currency, locale='en_US')
        converted_amount = format_currency(amount, to_currency, locale='en_US')
        return f"{formatted_amount} -> {converted_amount}"
    except Exception as e:
        return f"Conversion failed: {str(e)}"

# JSON representation of the code                
              
Tags: