mirror of
https://github.com/btcpayserver/btcpayserver.git
synced 2024-11-19 18:11:36 +01:00
ae32fdeea7
* Introduce Additional Data to Store Blob and move obsolete props to migration * Fixes and tests * Small adjustements to prevent tracking too many objects Co-authored-by: nicolas.dorier <nicolas.dorier@gmail.com>
45 lines
1.3 KiB
C#
45 lines
1.3 KiB
C#
using System;
|
|
using System.Globalization;
|
|
using System.Text.RegularExpressions;
|
|
using BTCPayServer.Services.Rates;
|
|
|
|
namespace BTCPayServer
|
|
{
|
|
public class CurrencyValue
|
|
{
|
|
static readonly Regex _Regex = new Regex("^([0-9]+(\\.[0-9]+)?)\\s*([a-zA-Z]+)$");
|
|
public static bool TryParse(string str, out CurrencyValue value)
|
|
{
|
|
value = null;
|
|
if (string.IsNullOrEmpty(str))
|
|
{
|
|
return false;
|
|
}
|
|
var match = _Regex.Match(str);
|
|
if (!match.Success ||
|
|
!decimal.TryParse(match.Groups[1].Value, out var v))
|
|
return false;
|
|
|
|
var currency = match.Groups[match.Groups.Count - 1].Value.ToUpperInvariant();
|
|
var currencyData = CurrencyNameTable.Instance.GetCurrencyData(currency, false);
|
|
if (currencyData == null)
|
|
return false;
|
|
v = Math.Round(v, currencyData.Divisibility);
|
|
value = new CurrencyValue()
|
|
{
|
|
Value = v,
|
|
Currency = currency
|
|
};
|
|
return true;
|
|
}
|
|
|
|
public decimal Value { get; set; }
|
|
public string Currency { get; set; }
|
|
|
|
public override string ToString()
|
|
{
|
|
return Value.ToString(CultureInfo.InvariantCulture) + " " + Currency;
|
|
}
|
|
}
|
|
}
|