Ejemplo n.º 1
0
        /// <summary>
        /// Format the amount in the specified base unit
        /// </summary>
        /// <param name="amountBase">The base unit to display the amount in</param>
        /// <returns></returns>
        public string ToString(AmountBase amountBase)
        {
            if (amountBase == AmountBase.raw)
            {
                return(Raw.ToString());
            }

            // Get the string as raw
            var rawString = Raw.ToString();

            // How many zeros to add
            var zeros = new String('0', (int)amountBase);

            var appended = zeros + rawString;

            // Move the decimal up by the base unit
            var withDecimal = appended.Substring(0, appended.Length - (int)amountBase) + "." + appended.Substring(appended.Length - (int)amountBase);

            // Trim off leaing zeros and trailing zeros and decimal points
            var trimmed = withDecimal.TrimStart('0').TrimEnd('0').TrimEnd('.');

            if (trimmed.Length == 0)
            {
                return("0");
            }

            if (trimmed[0] == '.')
            {
                return("0" + trimmed);
            }

            return(trimmed);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Validates if amount base entity contains the required information.
        /// </summary>
        /// <param name="entity">object</param>
        /// <param name="fields">list</param>
        /// <param name="silent">boold</param>
        /// <returns>bool</returns>
        public bool IsValid(object entity, out List <string> fields, bool silent)
        {
            List <string> errors     = new List <string>();
            AmountBase    amountBase = (AmountBase)entity;

            if (amountBase.Currency == null || !Currency.IsValidCurrency(amountBase.Currency))
            {
                errors.Add("currency");
            }

            if (amountBase.Total == 0)
            {
                errors.Add("total");
            }

            if (errors?.Any() ?? false)
            {
                fields = errors;
                ThrowValidationException(errors, "AmountBase", silent);

                return(false);
            }

            fields = null;

            return(true);
        }
Ejemplo n.º 3
0
 public BigInteger ConvertTo(AmountBase amountBase)
 {
     return(this.Raw / BigInteger.Pow(10, (int)amountBase));
 }
Ejemplo n.º 4
0
 /// <summary>
 /// Creates a <see cref="NanoAmount"/> of the specified amount and base unit
 /// </summary>
 /// <param name="amount">The numerical value of the amount</param>
 /// <param name="amountBase">The base unit of the amount</param>
 public NanoAmount(long amount, AmountBase amountBase)
 {
     Raw = amount * BigInteger.Pow(10, (int)amountBase);
 }