Exemplo n.º 1
0
 /// <summary>
 /// Convert Money to decimal (same as ToDecimal)
 /// </summary>
 /// <param name="unit"></param>
 /// <returns></returns>
 public decimal ToUnit(MoneyUnit unit)
 {
     CheckMoneyUnit(unit, "unit");
     // overflow safe because (long / int) always fit in decimal
     // decimal operations are checked by default
     return((decimal)Satoshi / (int)unit);
 }
Exemplo n.º 2
0
		private static void CheckMoneyUnit(MoneyUnit value, string paramName)
		{
			var typeOfMoneyUnit = typeof(MoneyUnit);
			if(!Enum.IsDefined(typeOfMoneyUnit, value))
			{
				throw new ArgumentException("Invalid value for MoneyUnit", paramName);
			}
		}
Exemplo n.º 3
0
 public Money(long amount, MoneyUnit unit)
 {
     // sanity check. Only valid units are allowed
     CheckMoneyUnit(unit, nameof(unit));
     checked
     {
         Satoshi = amount * (int)unit;
     }
 }
Exemplo n.º 4
0
 public Money(decimal amount, MoneyUnit unit)
 {
     // sanity check. Only valid units are allowed
     CheckMoneyUnit(unit, "unit");
     checked
     {
         var satoshi = amount * (int)unit;
         Satoshi = (long)satoshi;
     }
 }
 public MoneyUnits(string defaultUnit, MoneyUnit[] units)
 {
     this.Units       = units;
     this.DefaultUnit = this.Units.First(mu => mu.Name.ToLowerInvariant() == defaultUnit.ToLowerInvariant());
     this.AtomicUnit  = this.Units.FirstOrDefault(mu => mu.Multiplier == 1);
     if (this.AtomicUnit == null)
     {
         this.AtomicUnit = MoneyUnit.AtomicUnit;
     }
 }
Exemplo n.º 6
0
 public Money(decimal amount, MoneyUnit unit)
 {
     // sanity check. Only valid units are allowed
     //CheckMoneyUnit(unit, "unit");
     checked
     {
         decimal satoshi = amount * (int)unit.Multiplier;
         this.Satoshi = (long)satoshi;
     }
 }
Exemplo n.º 7
0
 private static void CheckMoneyLimits(decimal amount, MoneyUnit unit)
 {
     checked
     {
         var satoshis = amount * (int)unit;
         if (satoshis % 1 != 0)
         {
             throw new ArgumentOutOfRangeException("Invalid value for amount. Min unit is one satoshi.");
         }
     }
 }
Exemplo n.º 8
0
        public static MoneyUnit MoneyUnit(string moneyUnitName)
        {
            foreach (Network network in Network.GetNetworks())
            {
                if (network.MoneyUnits == null)
                {
                    continue;
                }

                MoneyUnit moneyUnit = network.MoneyUnits.GetMoneyUnit(moneyUnitName);

                if (moneyUnit != null)
                {
                    return(moneyUnit);
                }
            }

            throw new ArgumentOutOfRangeException("moneyUnitName", $"The '{moneyUnitName}' money unit is unknown among the networks.");
        }
Exemplo n.º 9
0
        public string Format(string format, object arg, IFormatProvider formatProvider)
        {
            if (!Equals(formatProvider))
            {
                return(null);
            }
            int  i    = 0;
            bool plus = format[i] == '+';

            if (plus)
            {
                i++;
            }
            int decPos = 0;

            if (int.TryParse(format.Substring(i, 1), out decPos))
            {
                i++;
            }
            var       unit            = format[i];
            MoneyUnit unitToUseInCalc = new MoneyUnit("BTC", 100000000);

            switch (unit)
            {
            case 'B':
                unitToUseInCalc = new MoneyUnit("BTC", 100000000);
                break;
            }
            var val   = Convert.ToDecimal(arg) / (int)unitToUseInCalc.Multiplier;
            var zeros = new string('0', decPos);
            var rest  = new string('#', 10 - decPos);
            var fmt   = plus && val > 0 ? "+" : string.Empty;

            fmt += "{0:0" + (decPos > 0 ? "." + zeros + rest : string.Empty) + "}";
            return(string.Format(CultureInfo.InvariantCulture, fmt, val));
        }
Exemplo n.º 10
0
 /// <summary>
 /// Convert Money to decimal (same as ToUnit)
 /// </summary>
 /// <param name="unit"></param>
 /// <returns></returns>
 public decimal ToDecimal(MoneyUnit unit)
 {
     return(ToUnit(unit));
 }
Exemplo n.º 11
0
 public static Money FromUnit(decimal amount, MoneyUnit unit)
 {
     return(new Money(amount, unit));
 }
Exemplo n.º 12
0
 public decimal ToUnit(MoneyUnit unit)
 {
     return((decimal)Satoshi / (int)unit);
 }
Exemplo n.º 13
0
 public Money(decimal amount, MoneyUnit unit)
 {
     _Satoshis = (long)(amount * (int)unit);
 }
Exemplo n.º 14
0
        public static bool TryParseDefault(Network network, string amount, out Money nRet)
        {
            nRet = null;

            Network[] networksToCheck = (network == null ? Network.GetNetworks().ToArray() : new Network[] { network });
            string[]  amountAndUnit   = amount.Split(' ');

            if ((amountAndUnit.Length != 1) && (amountAndUnit.Length != 2))
            {
                return(false);
            }

            MoneyUnit moneyUnit = null;

            if (amountAndUnit.Length == 2)
            {
                foreach (Network unitsNetwork in networksToCheck)
                {
                    if (unitsNetwork == null)
                    {
                        continue;
                    }

                    moneyUnit = unitsNetwork.MoneyUnits.Units.FirstOrDefault(mu => mu.Name.ToLowerInvariant() == amountAndUnit[1].ToLowerInvariant());
                    if (moneyUnit != null)
                    {
                        break;
                    }
                }
            }
            if (moneyUnit == null)
            {
                moneyUnit = MoneyUnit.AtomicUnit;
            }

            decimal value;

            if (!decimal.TryParse(amountAndUnit[0], BitcoinStyle, CultureInfo.InvariantCulture, out value))
            {
                return(false);
            }

            if (value < 0)
            {
                throw new FormatException("The money amount must be a positive number.");
                return(false);
            }

            if ((value * moneyUnit.Multiplier) - Math.Truncate(value * moneyUnit.Multiplier) > 0)
            {
                throw new FormatException("The money amount is defined in ambigous format. This could mean that you didn't use a unit name, or you used a value which is not properly expressable on the current network. Try to use unit names (like '0.12 BTC') or round the amount to have less decimal places. ");
                return(false);
            }

            try
            {
                nRet = new Money(value, moneyUnit);
                return(true);
            }
            catch (OverflowException)
            {
                return(false);
            }
            catch (ArgumentException)
            {
                return(false);
            }
        }