示例#1
0
        public decimal ReadDecimal()
        {
            DecimalTypeFlag flag = (DecimalTypeFlag)_reader.ReadByte();

            if (flag == DecimalTypeFlag.Zero)
            {
                return(default(decimal));
            }
            bool isMinus = (flag & DecimalTypeFlag.Minus) == DecimalTypeFlag.Minus;

            decimal ret = (decimal)_reader.ReadDouble();

            if (isMinus)
            {
                ret = -ret;
            }
            return(ret);
        }
示例#2
0
        public decimal ReadDecimal()
        {
            DecimalTypeFlag flag = (DecimalTypeFlag)_reader.ReadByte();

            if (flag == DecimalTypeFlag.Zero)
            {
                return(default(decimal));
            }
            bool isMinus = (flag & DecimalTypeFlag.Minus) == DecimalTypeFlag.Minus;

            decimal ret;

            if ((flag & DecimalTypeFlag.ByteVal) == DecimalTypeFlag.ByteVal)
            {
                ret = _reader.ReadByte();
            }
            else if ((flag & DecimalTypeFlag.ShortVal) == DecimalTypeFlag.ShortVal)
            {
                ret = _reader.ReadUInt16();
            }
            else if ((flag & DecimalTypeFlag.IntVal) == DecimalTypeFlag.IntVal)
            {
                ret = _reader.ReadUInt32();
            }
            else if ((flag & DecimalTypeFlag.Int64Val) == DecimalTypeFlag.Int64Val)
            {
                ret = _reader.ReadUInt64();
            }
            else if ((flag & DecimalTypeFlag.FloatVal) == DecimalTypeFlag.FloatVal)
            {
                ret = (decimal)_reader.ReadSingle();
            }
            else
            {
                ret = (decimal)_reader.ReadDouble();
            }
            if (isMinus)
            {
                ret = -ret;
            }
            return(ret);
        }
示例#3
0
        public void WriteDecimal(decimal data)
        {
            CheckBufferPoll(1);

            if (data == 0m)
            {
                _ms.WriteByte((byte)DecimalTypeFlag.Zero);
                return;
            }
            DecimalTypeFlag flag = DecimalTypeFlag.DEFAULT;

            if (data < 0)
            {
                flag = DecimalTypeFlag.Minus;
                data = Math.Abs(data);
            }

            flag |= DecimalTypeFlag.DoubleVal;
            byte[] byts = BitConverter.GetBytes((double)data);

            CheckBufferPoll(1 + byts.Length);
            _ms.WriteByte((byte)flag);
            _ms.Write(byts, 0, byts.Length);
        }
        public void WriteDecimal(decimal data)
        {
            CheckBufferPoll(1);

            if (data == 0m)
            {
                _ms.WriteByte((byte)DecimalTypeFlag.Zero);
                return;
            }
            DecimalTypeFlag flag = DecimalTypeFlag.DEFAULT;

            if (data < 0)
            {
                flag = DecimalTypeFlag.Minus;
                data = Math.Abs(data);
            }
            byte[]  byts = null;
            decimal mod  = data % 1;

            if (0 == mod)
            {
                if (data <= byte.MaxValue)
                {
                    flag |= DecimalTypeFlag.ByteVal;
                    byts  = new byte[] { (byte)data };
                }
                else if (data <= short.MaxValue)
                {
                    flag |= DecimalTypeFlag.ShortVal;
                    byts  = BitConverter.GetBytes((short)data);
                }
                else if (data <= int.MaxValue)
                {
                    flag |= DecimalTypeFlag.IntVal;
                    byts  = BitConverter.GetBytes((int)data);
                }
                else if (data <= long.MaxValue)
                {
                    flag |= DecimalTypeFlag.Int64Val;
                    byts  = BitConverter.GetBytes((long)data);
                }
            }
            else if ((mod * 10 % 1 == 0 && data <= 9999999) ||
                     (mod * 100 % 1 == 0 && data <= 999999) ||
                     (mod * 1000 % 1 == 0 && data <= 99999) ||
                     (mod * 10000 % 1 == 0 && data <= 9999) ||
                     (mod * 100000 % 1 == 0 && data <= 999) ||
                     (mod * 1000000 % 1 == 0 && data <= 99) ||
                     (mod * 10000000 % 1 == 0 && data <= 9))
            {
                flag |= DecimalTypeFlag.FloatVal;
                byts  = BitConverter.GetBytes((float)data);
            }
            else
            {
                flag |= DecimalTypeFlag.DoubleVal;
                byts  = BitConverter.GetBytes((double)data);
            }

            CheckBufferPoll(1 + byts.Length);
            _ms.WriteByte((byte)flag);
            _ms.Write(byts, 0, byts.Length);
        }