🛠️ToolsShed

Calcolatore di Tasso di Cambio

Converti un importo in più valute contemporaneamente.

I tassi sono valori di riferimento approssimativi. Usa un servizio live per le transazioni reali.

Domande Frequenti

Implementazione del Codice

def convert_currency(amount, from_rate_usd, to_rate_usd):
    """
    Convert amount between two currencies using USD as a base.

    Parameters:
        amount        - value to convert
        from_rate_usd - units of from-currency per 1 USD
        to_rate_usd   - units of to-currency per 1 USD

    Returns the converted amount.
    """
    amount_in_usd = amount / from_rate_usd
    return amount_in_usd * to_rate_usd


# Hardcoded reference rates (units per 1 USD)
RATES = {
    "USD": 1.0,
    "EUR": 0.92,
    "GBP": 0.79,
    "JPY": 149.50,
    "KRW": 1325.0,
    "CNY": 7.24,
    "CAD": 1.36,
    "AUD": 1.53,
    "CHF": 0.90,
    "INR": 83.10,
}


def convert(amount, from_currency, to_currency, rates=RATES):
    """Convert amount from one currency to another."""
    if from_currency not in rates or to_currency not in rates:
        raise ValueError("Unsupported currency code")
    return convert_currency(amount, rates[from_currency], rates[to_currency])


# Example: convert 100 EUR to JPY
result = convert(100, "EUR", "JPY")
print(f"100 EUR = {result:,.2f} JPY")

# Convert 1 USD to all currencies
for code, rate in RATES.items():
    print(f"1 USD = {rate:>10.4f} {code}")

Comments & Feedback

Comments are powered by Giscus. Sign in with GitHub to leave a comment.