Exemplo n.º 1
0
        /// <summary>
        /// Converts the current <see cref="AvroDecimal"/> to a string.
        /// </summary>
        /// <returns>A string representation of the numeric value.</returns>
        public override string ToString()
        {
            var number = UnscaledValue.ToString($"D{Scale + 1}", CultureInfo.CurrentCulture);

            if (Scale > 0)
            {
                return(number.Insert(number.Length - Scale, CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator));
            }

            return(number);
        }
Exemplo n.º 2
0
        public byte[] ToByteArray()
        {
            byte[] result = AllocateResult();

            byte[] data = UnscaledValue.ToByteArray();

            if (data.Length > result.Length)
            {
                throw new NotSupportedException($"decimal data buffer is {data.Length} but result must fit into {result.Length} bytes");
            }

            Array.Copy(data, result, data.Length);

            result = result.Reverse().ToArray();
            return(result);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Compares the value of the current <see cref="AvroDecimal"/> to the value of another
        /// <see cref="AvroDecimal"/>.
        /// </summary>
        /// <param name="other">The <see cref="AvroDecimal"/> to compare.</param>
        /// <returns>A value that indicates the relative order of the <see cref="AvroDecimal"/>
        /// instances being compared.</returns>
        public int CompareTo(AvroDecimal other)
        {
            var unscaledValueCompare = UnscaledValue.CompareTo(other.UnscaledValue);
            var scaleCompare         = Scale.CompareTo(other.Scale);

            // if both are the same value, return the value
            if (unscaledValueCompare == scaleCompare)
            {
                return(unscaledValueCompare);
            }

            // if the scales are both the same return unscaled value
            if (scaleCompare == 0)
            {
                return(unscaledValueCompare);
            }

            var scaledValue      = BigInteger.Divide(UnscaledValue, BigInteger.Pow(new BigInteger(10), Scale));
            var otherScaledValue = BigInteger.Divide(other.UnscaledValue, BigInteger.Pow(new BigInteger(10), other.Scale));

            return(scaledValue.CompareTo(otherScaledValue));
        }
Exemplo n.º 4
0
        public byte[] ToByteArray()
        {
            /*
             * Java: https://docs.oracle.com/javase/7/docs/api/java/math/BigInteger.html#toByteArray()
             *
             * Returns a byte array containing the two's-complement representation of this BigInteger. The byte array will be in big-endian byte-order: the most significant byte is in the zeroth element. The array will contain the minimum number of bytes required to represent this BigInteger, including at least one sign bit, which is (ceil((this.bitLength() + 1)/8)). (This representation is compatible with the (byte[]) constructor.)
             *
             * C#:   https://msdn.microsoft.com/en-us/library/system.numerics.biginteger.tobytearray(v=vs.110).aspx
             *
             *
             *  value | C# | Java
             *
             * -1 | [1111 1111] | [1111 1111] - no difference, so maybe buffer size?
             *
             */


            byte[] result = AllocateResult();

            byte[] data = UnscaledValue.ToByteArray();
            if (data.Length > result.Length)
            {
                throw new NotSupportedException($"decimal data buffer is {data.Length} but result must fit into {result.Length} bytes");
            }

            Array.Copy(data, result, data.Length);

            //if value is negative fill the remaining bytes with [1111 1111] i.e. negative flag bit (0xFF)
            if (UnscaledValue.Sign == -1)
            {
                for (int i = data.Length; i < result.Length; i++)
                {
                    result[i] = 0xFF;
                }
            }

            result = result.Reverse().ToArray();
            return(result);
        }
Exemplo n.º 5
0
 /// <summary>
 /// Returns the hash code for the current <see cref="AvroDecimal"/>.
 /// </summary>
 /// <returns>The hash code.</returns>
 public override int GetHashCode()
 {
     return(UnscaledValue.GetHashCode() ^ Scale.GetHashCode());
 }