btcpayserver/BTCPayServer/CurrencyValue.cs

45 lines
1.3 KiB
C#
Raw Normal View History

2020-06-29 04:44:35 +02:00
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;
2019-10-03 11:00:07 +02:00
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;
}
}
}