mirror of
https://github.com/aljazceru/btcpayserver.git
synced 2025-12-18 06:24:24 +01:00
Move currency display method into CurrencyNameTable
This commit is contained in:
@@ -332,7 +332,7 @@ namespace BTCPayServer.Tests
|
|||||||
(0.1m, "$0.10 (USD)"),
|
(0.1m, "$0.10 (USD)"),
|
||||||
})
|
})
|
||||||
{
|
{
|
||||||
var actual = InvoiceController.FormatCurrency(test.Item1, "USD", new CurrencyNameTable());
|
var actual = new CurrencyNameTable().DisplayFormatCurrency(test.Item1, "USD");
|
||||||
Assert.Equal(test.Item2, actual);
|
Assert.Equal(test.Item2, actual);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -57,7 +57,7 @@ namespace BTCPayServer.Controllers
|
|||||||
MonitoringDate = invoice.MonitoringExpiration,
|
MonitoringDate = invoice.MonitoringExpiration,
|
||||||
OrderId = invoice.OrderId,
|
OrderId = invoice.OrderId,
|
||||||
BuyerInformation = invoice.BuyerInformation,
|
BuyerInformation = invoice.BuyerInformation,
|
||||||
Fiat = FormatCurrency((decimal)dto.Price, dto.Currency, _CurrencyNameTable),
|
Fiat = _CurrencyNameTable.DisplayFormatCurrency((decimal)dto.Price, dto.Currency),
|
||||||
NotificationUrl = invoice.NotificationURL,
|
NotificationUrl = invoice.NotificationURL,
|
||||||
RedirectUrl = invoice.RedirectURL,
|
RedirectUrl = invoice.RedirectURL,
|
||||||
ProductInformation = invoice.ProductInformation,
|
ProductInformation = invoice.ProductInformation,
|
||||||
@@ -323,39 +323,12 @@ namespace BTCPayServer.Controllers
|
|||||||
if (cryptoCode == productInformation.Currency)
|
if (cryptoCode == productInformation.Currency)
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
return FormatCurrency(productInformation.Price, productInformation.Currency, _CurrencyNameTable);
|
return _CurrencyNameTable.DisplayFormatCurrency(productInformation.Price, productInformation.Currency);
|
||||||
}
|
}
|
||||||
private string ExchangeRate(PaymentMethod paymentMethod)
|
private string ExchangeRate(PaymentMethod paymentMethod)
|
||||||
{
|
{
|
||||||
string currency = paymentMethod.ParentEntity.ProductInformation.Currency;
|
string currency = paymentMethod.ParentEntity.ProductInformation.Currency;
|
||||||
return FormatCurrency(paymentMethod.Rate, currency, _CurrencyNameTable);
|
return _CurrencyNameTable.DisplayFormatCurrency(paymentMethod.Rate, currency);
|
||||||
}
|
|
||||||
|
|
||||||
public static string FormatCurrency(decimal price, string currency, CurrencyNameTable currencies)
|
|
||||||
{
|
|
||||||
var provider = currencies.GetNumberFormatInfo(currency, true);
|
|
||||||
var currencyData = currencies.GetCurrencyData(currency, true);
|
|
||||||
var divisibility = currencyData.Divisibility;
|
|
||||||
while (true)
|
|
||||||
{
|
|
||||||
var rounded = decimal.Round(price, divisibility, MidpointRounding.AwayFromZero);
|
|
||||||
if ((Math.Abs(rounded - price) / price) < 0.001m)
|
|
||||||
{
|
|
||||||
price = rounded;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
divisibility++;
|
|
||||||
}
|
|
||||||
if (divisibility != provider.CurrencyDecimalDigits)
|
|
||||||
{
|
|
||||||
provider = (NumberFormatInfo)provider.Clone();
|
|
||||||
provider.CurrencyDecimalDigits = divisibility;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (currencyData.Crypto)
|
|
||||||
return price.ToString("C", provider);
|
|
||||||
else
|
|
||||||
return price.ToString("C", provider) + $" ({currency})";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpGet]
|
[HttpGet]
|
||||||
|
|||||||
@@ -101,6 +101,40 @@ namespace BTCPayServer.Services.Rates
|
|||||||
currencyProviders.TryAdd(code, number);
|
currencyProviders.TryAdd(code, number);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Format a currency like "0.004 $ (USD)", round to significant divisibility
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="value">The value</param>
|
||||||
|
/// <param name="currency">Currency code</param>
|
||||||
|
/// <param name="threeLetterSuffix">Add three letter suffix (like USD)</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public string DisplayFormatCurrency(decimal value, string currency, bool threeLetterSuffix = true)
|
||||||
|
{
|
||||||
|
var provider = GetNumberFormatInfo(currency, true);
|
||||||
|
var currencyData = GetCurrencyData(currency, true);
|
||||||
|
var divisibility = currencyData.Divisibility;
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
var rounded = decimal.Round(value, divisibility, MidpointRounding.AwayFromZero);
|
||||||
|
if ((Math.Abs(rounded - value) / value) < 0.001m)
|
||||||
|
{
|
||||||
|
value = rounded;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
divisibility++;
|
||||||
|
}
|
||||||
|
if (divisibility != provider.CurrencyDecimalDigits)
|
||||||
|
{
|
||||||
|
provider = (NumberFormatInfo)provider.Clone();
|
||||||
|
provider.CurrencyDecimalDigits = divisibility;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (currencyData.Crypto)
|
||||||
|
return value.ToString("C", provider);
|
||||||
|
else
|
||||||
|
return value.ToString("C", provider) + $" ({currency})";
|
||||||
|
}
|
||||||
|
|
||||||
Dictionary<string, CurrencyData> _Currencies;
|
Dictionary<string, CurrencyData> _Currencies;
|
||||||
|
|
||||||
static CurrencyData[] LoadCurrency()
|
static CurrencyData[] LoadCurrency()
|
||||||
@@ -135,13 +169,16 @@ namespace BTCPayServer.Services.Rates
|
|||||||
|
|
||||||
foreach (var network in new BTCPayNetworkProvider(NetworkType.Mainnet).GetAll())
|
foreach (var network in new BTCPayNetworkProvider(NetworkType.Mainnet).GetAll())
|
||||||
{
|
{
|
||||||
dico.TryAdd(network.CryptoCode, new CurrencyData()
|
if (!dico.TryAdd(network.CryptoCode, new CurrencyData()
|
||||||
{
|
{
|
||||||
Code = network.CryptoCode,
|
Code = network.CryptoCode,
|
||||||
Divisibility = 8,
|
Divisibility = 8,
|
||||||
Name = network.CryptoCode,
|
Name = network.CryptoCode,
|
||||||
Crypto = true
|
Crypto = true
|
||||||
});
|
}))
|
||||||
|
{
|
||||||
|
dico[network.CryptoCode].Crypto = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return dico.Values.ToArray();
|
return dico.Values.ToArray();
|
||||||
|
|||||||
Reference in New Issue
Block a user