CurrencyConverter.cs
1,004 bytes
| 1 | namespace App.BLL.Helpers; |
|---|---|
| 2 | |
| 3 | public static class CurrencyConverter |
| 4 | { |
| 5 | // Rates: 1 unit of currency = X EUR (approximate, hardcoded) |
| 6 | private static readonly Dictionary<string, decimal> RatesToEur = new() |
| 7 | { |
| 8 | { "EUR", 1.0m }, |
| 9 | { "USD", 0.92m }, |
| 10 | { "GBP", 1.16m }, |
| 11 | { "SEK", 0.087m }, |
| 12 | { "NOK", 0.086m }, |
| 13 | }; |
| 14 | |
| 15 | /// <summary> |
| 16 | /// Convert amount from one currency to another. |
| 17 | /// Falls back to 1:1 if either currency code is unknown. |
| 18 | /// </summary> |
| 19 | public static decimal Convert(decimal amount, string fromCurrencyCode, string toCurrencyCode) |
| 20 | { |
| 21 | if (fromCurrencyCode == toCurrencyCode) return amount; |
| 22 | |
| 23 | if (!RatesToEur.TryGetValue(fromCurrencyCode, out var fromRate)) return amount; |
| 24 | if (!RatesToEur.TryGetValue(toCurrencyCode, out var toRate)) return amount; |
| 25 | |
| 26 | // Convert: amount in "from" -> EUR -> "to" |
| 27 | var amountInEur = amount * fromRate; |
| 28 | return Math.Round(amountInEur / toRate, 2); |
| 29 | } |
| 30 | } |
| 31 | |