সিম্পল গুণফল দিয়ে একটি মুদ্রা থেকে অন্য মুদ্রায় কনভার্ট করা যায়। বাস্তবে এক্সচেঞ্জ রেট API থেকে ফেচ করা উচিত।
বেসিক ফর্মুলা
double usd = 10; // ১০ ডলার
double rate = 69; // ১ USD = ৬৯ INR (উদাহরণ রেট)
double inr = usd * rate; // ৬৯০ INR
সম্পূর্ণ উদাহরণ
using System;
class CurrencyConverterDemo
{
public static void Main()
{
double usd = 10;
double rate = 69; // এক্সচেঞ্জ রেট (উদাহরণ)
double inr = usd * rate;
Console.WriteLine($"{usd} USD = {inr} INR (রেট: {rate})");
// রিয়েল-টাইম API কলের জন্য HttpClient ব্যবহার করুন
// var client = new HttpClient();
// var rate = await client.GetFromJsonAsync("https://api.exchangerate-api.com/v4/latest/USD");
}
}
আউটপুট
10 USD = 690 INR (রেট: 69)
উন্নত সংস্করণ: 제네রিক কনভার্টার
public class CurrencyConverter
{
private readonly Dictionary<string, decimal> _rates;
public CurrencyConverter(Dictionary<string, decimal> rates)
{
_rates = rates;
}
public decimal Convert(decimal amount, string from, string to)
{
if (from == to) return amount;
if (!_rates.ContainsKey(from) || !_rates.ContainsKey(to))
throw new ArgumentException("Unsupported currency");
// Base currency (e.g., USD) melalui কনভার্ট
decimal fromRate = _rates[from];
decimal toRate = _rates[to];
return amount * (toRate / fromRate);
}
}
বাস্তব ব্যবহারের জন্য API
- exchangerate-api.com, fixer.io, openexchangerates.org
- ক্যাশিং করুন (রেট দিনে একবার আপডেট হয়)।
- Decimal টাইপ ব্যবহার করুন (ফ্লোটিং পয়েন্ট এরর এড়াতে)।