// public methods
        /// <summary>
        /// Converts a Decimal128 to a Decimal.
        /// </summary>
        /// <param name="value">A Decimal128.</param>
        /// <returns>A Decimal.</returns>
        public decimal ToDecimal(Decimal128 value)
        {
            if (value == Decimal128.MaxValue)
            {
                return decimal.MaxValue;
            }
            else if (value == Decimal128.MinValue)
            {
                return decimal.MinValue;
            }
            else if (Decimal128.IsInfinity(value) || Decimal128.IsNaN(value))
            {
                throw new OverflowException();
            }

            decimal decimalValue;
            if (_allowOverflow)
            {
                try { decimalValue = (decimal)value; } catch (OverflowException) { decimalValue = Decimal128.IsNegative(value) ? decimal.MinValue : decimal.MaxValue; }
            }
            else
            {
                decimalValue = (decimal)value;
            }

            if (!_allowTruncation && value != (Decimal128)decimalValue)
            {
                throw new TruncationException();
            }

            return decimalValue;
        }
        public void Byte(byte value, string s)
        {
            var subject = new Decimal128(value);

            subject.ToString().Should().Be(s);
            AssertSpecialProperties(subject);

            var result = Decimal128.ToByte(subject);
            result.Should().Be(value);

            result = (byte)subject;
            result.Should().Be(value);
        }
        public void Decimal(string valueString, string s)
        {
            var value = decimal.Parse(valueString);
            var subject = new Decimal128(value);

            subject.ToString().Should().Be(s);
            AssertSpecialProperties(subject);

            var result = Decimal128.ToDecimal(subject);
            result.Should().Be(value);

            result = (decimal)subject;
            result.Should().Be(value);
        }
        public void ToSingle_should_not_depend_on_current_culture(string valueString, float expectedFloat)
        {
            var currentCulture = Thread.CurrentThread.CurrentCulture;

            try
            {
                Thread.CurrentThread.CurrentCulture = new CultureInfo("fr-FR");
                var subject = Decimal128.Parse(valueString);

                var result = Decimal128.ToSingle(subject);

                result.Should().Be(expectedFloat);
            }
            finally
            {
                Thread.CurrentThread.CurrentCulture = currentCulture;
            }
        }
Exemple #5
0
        /// <summary>
        /// Converts a Decimal128 to an Int32.
        /// </summary>
        /// <param name="value">A Decimal128.</param>
        /// <returns>An Int32.</returns>
        public int ToInt32(Decimal128 value)
        {
            int intValue;

            if (_allowOverflow)
            {
                try { intValue = (int)value; } catch (OverflowException) { intValue = Decimal128.IsNegative(value) ? int.MinValue : int.MaxValue; }
            }
            else
            {
                intValue = (int)value;
            }
            if (!_allowTruncation && value != (Decimal128)intValue)
            {
                throw new TruncationException();
            }
            return(intValue);
        }
Exemple #6
0
        /// <summary>
        /// Converts a Decimal128 to an Int64.
        /// </summary>
        /// <param name="value">A Decimal128.</param>
        /// <returns>An Int64.</returns>
        public long ToInt64(Decimal128 value)
        {
            long longValue;

            if (_allowOverflow)
            {
                try { longValue = (long)value; } catch (OverflowException) { longValue = Decimal128.IsNegative(value) ? long.MinValue : long.MaxValue; }
            }
            else
            {
                longValue = (long)value;
            }
            if (!_allowTruncation && value != (Decimal128)longValue)
            {
                throw new TruncationException();
            }
            return(longValue);
        }
Exemple #7
0
        /// <summary>
        ///     初始化
        /// </summary>
        /// <param name="value"></param>
        public BsonValueEx(BsonValue value)
        {
            if (value.IsString)
            {
                mBsonType   = BasicType.BsonString;
                mBsonString = value.ToString();
            }
            if (value.IsInt32)
            {
                mBsonType  = BasicType.BsonInt32;
                mBsonInt32 = value.AsInt32;
            }

            if (value.IsInt64)
            {
                mBsonType       = BasicType.BsonInt64;
                mBSonDecimal128 = value.AsDecimal;
            }

            if (value.IsDecimal128)
            {
                mBsonType       = BasicType.BsonDecimal128;
                mBSonDecimal128 = value.AsDecimal;
            }

            if (value.IsDouble)
            {
                mBsonType   = BasicType.BsonDouble;
                mBsonDouble = value.AsDouble;
            }

            if (value.IsValidDateTime)
            {
                mBsonType     = BasicType.BsonDateTime;
                mBsonDateTime = value.ToUniversalTime();
            }

            if (value.IsBoolean)
            {
                mBsonType    = BasicType.BsonBoolean;
                mBsonBoolean = value.AsBoolean;
            }
        }
Exemple #8
0
        public void Decimal128Tests(Decimal128 value, int expectGreater, int expectGreaterEqual, int expectLess, int expectLessEqual)
        {
            SeedDecimal128Data();

            var greater = _realm.All <DecimalsObject>().Count(d => d.Decimal128Value > value);

            Assert.That(greater, Is.EqualTo(expectGreater));

            var greaterEqual = _realm.All <DecimalsObject>().Count(d => d.Decimal128Value >= value);

            Assert.That(greaterEqual, Is.EqualTo(expectGreaterEqual));

            var less = _realm.All <DecimalsObject>().Count(d => d.Decimal128Value < value);

            Assert.That(less, Is.EqualTo(expectLess));

            var lessEqual = _realm.All <DecimalsObject>().Count(d => d.Decimal128Value <= value);

            Assert.That(lessEqual, Is.EqualTo(expectLessEqual));
        }
Exemple #9
0
        public uint ToUInt32(Decimal128 value)
        {
            uint uintValue;

            if (_allowOverflow)
            {
                try { uintValue = (uint)value; } catch (OverflowException) { uintValue = Decimal128.IsNegative(value) ? uint.MinValue : uint.MaxValue; }
            }
            else
            {
                uintValue = (uint)value;
            }

            if (!_allowTruncation && value != (Decimal128)uintValue)
            {
                throw new TruncationException();
            }

            return(uintValue);
        }
Exemple #10
0
        public ulong ToUInt64(Decimal128 value)
        {
            ulong ulongValue;

            if (_allowOverflow)
            {
                try { ulongValue = (ulong)value; } catch (OverflowException) { ulongValue = Decimal128.IsNegative(value) ? ulong.MinValue : ulong.MaxValue; }
            }
            else
            {
                ulongValue = (ulong)value;
            }

            if (!_allowTruncation && value != (Decimal128)ulongValue)
            {
                throw new TruncationException();
            }

            return(ulongValue);
        }
Exemple #11
0
        /// <summary>
        /// Converts a Decimal128 to an Int16.
        /// </summary>
        /// <param name="value">A Decimal128.</param>
        /// <returns>An Int16.</returns>
        public short ToInt16(Decimal128 value)
        {
            short shortValue;

            if (_allowOverflow)
            {
                try { shortValue = (short)value; } catch (OverflowException) { shortValue = Decimal128.IsNegative(value) ? short.MinValue : short.MaxValue; }
            }
            else
            {
                shortValue = (short)value;
            }

            if (!_allowTruncation && value != (Decimal128)shortValue)
            {
                throw new TruncationException();
            }

            return(shortValue);
        }
Exemple #12
0
        public void PlayWithDecimals()
        {
            // :hide-start:
            var app    = App.Create("tuts-tijya");
            var user   = app.LogInAsync(Credentials.EmailPassword("*****@*****.**", "foobar")).Result;
            var config = new SyncConfiguration("myPart", user);
            var realm  = Realm.GetInstanceAsync().Result;
            // :hide-end:
            var myInstance = new MyClassWithDecimals();

            // To store decimal values:
            realm.Write(() =>
            {
                myInstance.VeryPreciseNumber     = 1.234567890123456789M;
                myInstance.EvenMorePreciseNumber = Decimal128.Parse("987654321.123456789");

                // Decimal128 has explicit constructors that take a float or a double
                myInstance.EvenMorePreciseNumber = new Decimal128(9.99999);
            });
        }
Exemple #13
0
        static object ReadObject(IBsonReader bsonReader)         //_120509_173140 sync, test
        {
            switch (bsonReader.GetCurrentBsonType())
            {
            case BsonType.Array: return(ReadArray(bsonReader));                    // replacement

            case BsonType.Boolean: return(bsonReader.ReadBoolean());

            case BsonType.DateTime: return(BsonUtils.ToDateTimeFromMillisecondsSinceEpoch(bsonReader.ReadDateTime()));

            case BsonType.Decimal128: return(Decimal128.ToDecimal(bsonReader.ReadDecimal128()));

            case BsonType.Document: return(ReadCustomObject(bsonReader));                    // replacement

            case BsonType.Double: return(bsonReader.ReadDouble());

            case BsonType.Int32: return(bsonReader.ReadInt32());

            case BsonType.Int64: return(bsonReader.ReadInt64());

            case BsonType.Null: bsonReader.ReadNull(); return(null);

            case BsonType.ObjectId: return(bsonReader.ReadObjectId());

            case BsonType.String: return(bsonReader.ReadString());

            case BsonType.Binary:
                var data = bsonReader.ReadBinaryData();
                switch (data.SubType)
                {
                case BsonBinarySubType.UuidLegacy:
                case BsonBinarySubType.UuidStandard:
                    return(data.ToGuid());

                default:
                    return(data);
                }

            default: return(BsonSerializer.Deserialize <BsonValue>(bsonReader));
            }
        }
Exemple #14
0
        /// <summary>
        /// Converts a Decimal128 to a Single.
        /// </summary>
        /// <param name="value">A Decimal128.</param>
        /// <returns>A Single.</returns>
        public float ToSingle(Decimal128 value)
        {
            if (value == Decimal128.MaxValue)
            {
                return(float.MaxValue);
            }
            else if (value == Decimal128.MinValue)
            {
                return(float.MinValue);
            }
            else if (Decimal128.IsPositiveInfinity(value))
            {
                return(float.PositiveInfinity);
            }
            else if (Decimal128.IsNegativeInfinity(value))
            {
                return(float.NegativeInfinity);
            }
            else if (Decimal128.IsNaN(value))
            {
                return(float.NaN);
            }

            float floatValue;

            if (_allowOverflow)
            {
                try { floatValue = (float)value; } catch (OverflowException) { floatValue = Decimal128.IsNegative(value) ? float.MinValue : float.MaxValue; }
            }
            else
            {
                floatValue = (float)value;
            }

            if (!_allowTruncation && value != (Decimal128)floatValue)
            {
                throw new TruncationException();
            }

            return(floatValue);
        }
Exemple #15
0
        /// <summary>
        /// Converts a Decimal128 to a Double.
        /// </summary>
        /// <param name="value">A Decimal.</param>
        /// <returns>A Double.</returns>
        public double ToDouble(Decimal128 value)
        {
            if (value == Decimal128.MaxValue)
            {
                return(double.MaxValue);
            }
            else if (value == Decimal128.MinValue)
            {
                return(double.MinValue);
            }
            else if (Decimal128.IsPositiveInfinity(value))
            {
                return(double.PositiveInfinity);
            }
            else if (Decimal128.IsNegativeInfinity(value))
            {
                return(double.NegativeInfinity);
            }
            else if (Decimal128.IsNaN(value))
            {
                return(double.NaN);
            }

            double doubleValue;

            if (_allowOverflow)
            {
                try { doubleValue = (double)value; } catch (OverflowException) { doubleValue = Decimal128.IsNegative(value) ? double.MinValue : double.MaxValue; }
            }
            else
            {
                doubleValue = (double)value;
            }

            if (!_allowTruncation && value != (Decimal128)doubleValue)
            {
                throw new TruncationException();
            }

            return(doubleValue);
        }
Exemple #16
0
        public void Decimal128Tests(Decimal128 value, bool isManaged)
        {
            RealmValue rv = value;

            if (isManaged)
            {
                var retrievedObject = PersistAndFind(rv);
                rv = retrievedObject.RealmValueProperty;
            }

            Assert.That(rv == value);
            Assert.That(rv.Type, Is.EqualTo(RealmValueType.Decimal128));

            Assert.That((Decimal128)rv == value);
            Assert.That(rv.As <Decimal128>() == value);
            Assert.That((Decimal128?)rv == value);
            Assert.That(rv.As <Decimal128?>() == value);
            Assert.That(rv.AsDecimal128() == value);
            Assert.That(rv.AsNullableDecimal128() == value);
            Assert.That(rv != RealmValue.Null);
        }
        public static void TestRoundTrip(int scale)
        {
            var list = new List<decimal>{0, 1};
            for (int i = 0; i != 28; ++i)
            {
                list.Add(list.Last() * 10);
            }

            list.Add(decimal.MaxValue);

            var multiplier = Decimal128.GetScaleMultiplier(scale);
            var decimals = list.Select(v => v / multiplier).ToArray();

            foreach (var value in decimals)
            {
                Console.WriteLine($"{value:E}");
                Assert.AreEqual(value, new Decimal128(value, multiplier).ToDecimal(multiplier));

                Console.WriteLine($"{-value:E}");
                Assert.AreEqual(-value, new Decimal128(-value, multiplier).ToDecimal(multiplier));
            }
        }
Exemple #18
0
        protected virtual decimal readAmount(IBsonReader reader)
        {
            BsonMemberMap amountMap = _map.GetMemberMap(m => m.Amount);

            reader.ReadName(amountMap.ElementName);
            decimal amount;
            // support old numeric representations
            BsonType type = reader.GetCurrentBsonType();

            switch (type)
            {
            case BsonType.Double:
            {
                double amountRead = reader.ReadDouble();
                amount = Convert.ToDecimal(amountRead);
                break;
            }

            case BsonType.Decimal128:
            {
                Decimal128 amountRead = reader.ReadDecimal128();
                amount = Decimal128.ToDecimal(amountRead);
                break;
            }

            case BsonType.String:
            {
                string amountRead = reader.ReadString();
                amount = decimal.Parse(amountRead, CultureInfo.InvariantCulture);
                break;
            }

            default:
                var message = $"Cannot convert a {type} to a Decimal.";
                throw new NotSupportedException(message);
            }

            return(amount);
        }
Exemple #19
0
    public void PlayWithDecimals()
    {
        var myInstance = new MyClassWithDecimals();

        // To store decimal values:
        realm.Write(() =>
        {
            myInstance.VeryPreciseNumber     = 1.234567890123456789M;
            myInstance.EvenMorePreciseNumber = Decimal128.Parse("987654321.123456789");

            // Decimal128 has explicit constructors that take a float or a double
            myInstance.EvenMorePreciseNumber = new Decimal128(9.99999);
        });

        // To query decimal values:
        var largerThanFive = realm.All <MyClassWithDecimals>()
                             .Where(o => o.VeryPreciseNumber > 5)
                             .OrderBy(o => o.EvenMorePreciseNumber);

        var smallerThanZero = realm.All <MyClassWithDecimals>()
                              .Where(o => o.EvenMorePreciseNumber < 0)
                              .OrderByDescending(o => o.VeryPreciseNumber);
    }
Exemple #20
0
        /// <summary>
        ///     初始化
        /// </summary>
        /// <param name="value"></param>
        public BsonValueEx(BsonValue value)
        {
            if (value.IsString)
            {
                MBsonType   = "BsonString";
                MBsonString = value.ToString();
            }
            if (value.IsInt32)
            {
                MBsonType  = "BsonInt32";
                MBsonInt32 = value.AsInt32;
            }

            if (value.IsInt64)
            {
                MBsonType       = "BsonInt64";
                MBSonDecimal128 = value.AsDecimal;
            }

            if (value.IsDecimal128)
            {
                MBsonType       = "BsonDecimal128";
                MBSonDecimal128 = value.AsDecimal;
            }

            if (value.IsValidDateTime)
            {
                MBsonType     = "BsonDateTime";
                MBsonDateTime = value.ToUniversalTime();
            }
            if (value.IsBoolean)
            {
                MBsonType    = "BsonBoolean";
                MBsonBoolean = value.AsBoolean;
            }
        }
 /// <inheritdoc />
 public override int ToInt32()
 {
     return(Decimal128.ToInt32(_value));
 }
 /// <inheritdoc />
 public override decimal ToDecimal()
 {
     return(Decimal128.ToDecimal(_value));
 }
 /// <summary>
 /// Writes a BSON Decimal128 element to the writer.
 /// </summary>
 /// <param name="writer">The writer.</param>
 /// <param name="name">The name of the element.</param>
 /// <param name="value">The <see cref="Decimal128"/> value.</param>
 public static void WriteDecimal128(this IBsonWriter writer, string name, Decimal128 value)
 {
     writer.WriteName(name);
     writer.WriteDecimal128(value);
 }
        /// <summary>
        /// Converts a Decimal128 to a Double.
        /// </summary>
        /// <param name="value">A Decimal.</param>
        /// <returns>A Double.</returns>
        public double ToDouble(Decimal128 value)
        {
            if (value == Decimal128.MaxValue)
            {
                return double.MaxValue;
            }
            else if (value == Decimal128.MinValue)
            {
                return double.MinValue;
            }
            else if (Decimal128.IsPositiveInfinity(value))
            {
                return double.PositiveInfinity;
            }
            else if (Decimal128.IsNegativeInfinity(value))
            {
                return double.NegativeInfinity;
            }
            else if (Decimal128.IsNaN(value))
            {
                return double.NaN;
            }

            double doubleValue;
            if (_allowOverflow)
            {
                try { doubleValue = (double)value; } catch (OverflowException) { doubleValue = Decimal128.IsNegative(value) ? double.MinValue : double.MaxValue; }
            }
            else
            {
                doubleValue = (double)value;
            }

            if (!_allowTruncation && value != (Decimal128)doubleValue)
            {
                throw new TruncationException();
            }

            return doubleValue;
        }
 /// <summary>
 /// Converts a <see cref="Decimal128"/> to a string.
 /// </summary>
 /// <param name="value">The value.</param>
 /// <returns>A string.</returns>
 public static string ToString(Decimal128 value)
 {
     return value.ToString();
 }
        /// <inheritdoc />
        public override void WriteDecimal128(Decimal128 value)
        {
            if (Disposed) { throw new ObjectDisposedException("BsonDocumentWriter"); }
            if (State != BsonWriterState.Value)
            {
                ThrowInvalidState(nameof(WriteDecimal128), BsonWriterState.Value);
            }

            WriteValue(new BsonDecimal128(value));
            State = GetNextState();
        }
        public ushort ToUInt16(Decimal128 value)
        {
            ushort ushortValue;
            if (_allowOverflow)
            {
                try { ushortValue = (ushort)value; } catch (OverflowException) { ushortValue = Decimal128.IsNegative(value) ? ushort.MinValue : ushort.MaxValue; }
            }
            else
            {
                ushortValue = (ushort)value;
            }

            if (!_allowTruncation && value != (Decimal128)ushortValue)
            {
                throw new TruncationException();
            }

            return ushortValue;
        }
        /// <summary>
        /// Converts a Decimal128 to a Single.
        /// </summary>
        /// <param name="value">A Decimal128.</param>
        /// <returns>A Single.</returns>
        public float ToSingle(Decimal128 value)
        {
            if (value == Decimal128.MaxValue)
            {
                return float.MaxValue;
            }
            else if (value == Decimal128.MinValue)
            {
                return float.MinValue;
            }
            else if (Decimal128.IsPositiveInfinity(value))
            {
                return float.PositiveInfinity;
            }
            else if (Decimal128.IsNegativeInfinity(value))
            {
                return float.NegativeInfinity;
            }
            else if (Decimal128.IsNaN(value))
            {
                return float.NaN;
            }

            float floatValue;
            if (_allowOverflow)
            {
                try { floatValue = (float)value; } catch (OverflowException) { floatValue = Decimal128.IsNegative(value) ? float.MinValue : float.MaxValue; }
            }
            else
            {
                floatValue = (float)value;
            }

            if (!_allowTruncation && value != (Decimal128)floatValue)
            {
                throw new TruncationException();
            }

            return floatValue;
        }
 /// <summary>
 /// Converts a Decimal128 to an Int64.
 /// </summary>
 /// <param name="value">A Decimal128.</param>
 /// <returns>An Int64.</returns>
 public long ToInt64(Decimal128 value)
 {
     long longValue;
     if (_allowOverflow)
     {
         try { longValue = (long)value; } catch (OverflowException) { longValue = Decimal128.IsNegative(value) ? long.MinValue : long.MaxValue; }
     }
     else
     {
         longValue = (long)value;
     }
     if (!_allowTruncation && value != (Decimal128)longValue)
     {
         throw new TruncationException();
     }
     return longValue;
 }
 /// <summary>
 /// Converts a Decimal128 to an Int32.
 /// </summary>
 /// <param name="value">A Decimal128.</param>
 /// <returns>An Int32.</returns>
 public int ToInt32(Decimal128 value)
 {
     int intValue;
     if (_allowOverflow)
     {
         try { intValue = (int)value; } catch (OverflowException) { intValue = Decimal128.IsNegative(value) ? int.MinValue : int.MaxValue; }
     }
     else
     {
         intValue = (int)value;
     }
     if (!_allowTruncation && value != (Decimal128)intValue)
     {
         throw new TruncationException();
     }
     return intValue;
 }
        /// <summary>
        /// Converts a Decimal128 to an Int16.
        /// </summary>
        /// <param name="value">A Decimal128.</param>
        /// <returns>An Int16.</returns>
        public short ToInt16(Decimal128 value)
        {
            short shortValue;
            if (_allowOverflow)
            {
                try { shortValue = (short)value; } catch (OverflowException) { shortValue = Decimal128.IsNegative(value) ? short.MinValue : short.MaxValue; }
            }
            else
            {
                shortValue = (short)value;
            }

            if (!_allowTruncation && value != (Decimal128)shortValue)
            {
                throw new TruncationException();
            }

            return shortValue;
        }
 /// <inheritdoc />
 public virtual void WriteDecimal128(Decimal128 value)
 {
     ThrowIfDisposed();
     _wrapped.WriteDecimal128(value);
 }
        public void Int16(short value, string s)
        {
            var subject = new Decimal128(value);

            subject.ToString().Should().Be(s);
            AssertSpecialProperties(subject);

            var result = Decimal128.ToInt16(subject);
            result.Should().Be(value);

            result = (short)subject;
            result.Should().Be(value);
        }
Exemple #34
0
        /// <summary>
        ///     初始化
        /// </summary>
        /// <param name="value"></param>
        public BsonValueEx(BsonValue value)
        {
            if (value.IsString)
            {
                mBsonType = BasicType.BsonString;
                mBsonString = value.ToString();
            }

            if (value.IsInt32)
            {
                mBsonType = BasicType.BsonInt32;
                mBsonInt32 = value.AsInt32;
            }
            if (value.IsInt64)
            {
                mBsonType = BasicType.BsonInt64;
                mBsonInt64 = value.AsInt64;
            }

            if (value.IsDecimal128)
            {
                mBsonType = BasicType.BsonDecimal128;
                mBSonDecimal128 = value.AsDecimal;
            }
            if (value.IsDouble)
            {
                mBsonType = BasicType.BsonDouble;
                mBsonDouble = value.AsDouble;
            }

            if (value.IsValidDateTime)
            {
                mBsonType = BasicType.BsonDateTime;
                mBsonDateTime = value.ToUniversalTime();
            }
            if (value.IsBoolean)
            {
                mBsonType = BasicType.BsonBoolean;
                mBsonBoolean = value.AsBoolean;
            }

            if (value.IsBsonMaxKey)
            {
                mBsonType = BasicType.BsonMaxKey;
            }
            if (value.IsBsonMinKey)
            {
                mBsonType = BasicType.BsonMinKey;
            }

            if (value.IsBsonBinaryData)
            {
                mBsonType = BasicType.BsonBinary;
                mBsonBinary = value.AsBsonBinaryData.Bytes;
            }
        }
 public Decimal128 AsDecimal() => Decimal128.FromIEEEBits(decimal_bits[1], decimal_bits[0]);
Exemple #36
0
        // public methods
        /// <summary>
        /// Converts a Decimal128 to a Decimal.
        /// </summary>
        /// <param name="value">A Decimal128.</param>
        /// <returns>A Decimal.</returns>
        public decimal ToDecimal(Decimal128 value)
        {
            if (value == Decimal128.MaxValue)
            {
                return(decimal.MaxValue);
            }
            else if (value == Decimal128.MinValue)
            {
                return(decimal.MinValue);
            }
            else if (Decimal128.IsInfinity(value) || Decimal128.IsNaN(value))
            {
                throw new OverflowException();
            }

            decimal decimalValue;

            if (_allowOverflow)
            {
                try { decimalValue = (decimal)value; } catch (OverflowException) { decimalValue = Decimal128.IsNegative(value) ? decimal.MinValue : decimal.MaxValue; }
            }
            else
            {
                decimalValue = (decimal)value;
            }

            if (!_allowTruncation && value != (Decimal128)decimalValue)
            {
                throw new TruncationException();
            }

            return(decimalValue);
        }
 /// <summary>
 /// Writes a BSON Decimal128 element to the writer.
 /// </summary>
 /// <param name="writer">The writer.</param>
 /// <param name="name">The name of the element.</param>
 /// <param name="value">The <see cref="Decimal128"/> value.</param>
 public static void WriteDecimal128(this IBsonWriter writer, string name, Decimal128 value)
 {
     writer.WriteName(name);
     writer.WriteDecimal128(value);
 }
        public uint ToUInt32(Decimal128 value)
        {
            uint uintValue;
            if (_allowOverflow)
            {
                try { uintValue = (uint)value; } catch (OverflowException) { uintValue = Decimal128.IsNegative(value) ? uint.MinValue : uint.MaxValue; }
            }
            else
            {
                uintValue = (uint)value;
            }

            if (!_allowTruncation && value != (Decimal128)uintValue)
            {
                throw new TruncationException();
            }

            return uintValue;
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="BsonDecimal128" /> class.
 /// </summary>
 /// <param name="value">The value.</param>
 public BsonDecimal128(Decimal128 value)
 {
     _value = value;
 }
        public ulong ToUInt64(Decimal128 value)
        {
            ulong ulongValue;
            if (_allowOverflow)
            {
                try { ulongValue = (ulong)value; } catch (OverflowException) { ulongValue = Decimal128.IsNegative(value) ? ulong.MinValue : ulong.MaxValue; }
            }
            else
            {
                ulongValue = (ulong)value;
            }

            if (!_allowTruncation && value != (Decimal128)ulongValue)
            {
                throw new TruncationException();
            }

            return ulongValue;
        }
        private BsonValue ParseNumberDecimalExtendedJson()
        {
            VerifyToken(":");

            Decimal128 value;
            var valueToken = PopToken();
            if (valueToken.Type == JsonTokenType.String)
            {
                value = Decimal128.Parse(valueToken.StringValue);
            }
            else if (valueToken.Type == JsonTokenType.Int32 || valueToken.Type == JsonTokenType.Int64)
            {
                value = new Decimal128(valueToken.Int64Value);
            }
            else
            {
                var message = string.Format("JSON reader expected a string or an integer but found '{0}'.", valueToken.Lexeme);
                throw new FormatException(message);
            }

            VerifyToken("}");
            return (BsonDecimal128)value;
        }
 private static RealmValue Decimal(Decimal128 value) => new RealmValue(PrimitiveValue.Decimal(value));
        /// <inheritdoc />
        public override void WriteDecimal128(Decimal128 value)
        {
            if (Disposed) { throw new ObjectDisposedException("BsonBinaryWriter"); }
            if (State != BsonWriterState.Value)
            {
                ThrowInvalidState(nameof(WriteDecimal128), BsonWriterState.Value);
            }

            _bsonStream.WriteBsonType(BsonType.Decimal128);
            WriteNameHelper();
            _bsonStream.WriteDecimal128(value);

            State = GetNextState();
        }
 private void AssertSpecialProperties(Decimal128 subject, bool qNaN = false, bool sNaN = false, bool posInfinity = false, bool negInfinity = false)
 {
     Decimal128.IsNaN(subject).Should().Be(qNaN || sNaN);
     Decimal128.IsQNaN(subject).Should().Be(qNaN);
     Decimal128.IsSNaN(subject).Should().Be(sNaN);
     Decimal128.IsInfinity(subject).Should().Be(posInfinity || negInfinity);
     Decimal128.IsNegativeInfinity(subject).Should().Be(negInfinity);
     Decimal128.IsPositiveInfinity(subject).Should().Be(posInfinity);
 }
Exemple #45
0
        private static unsafe object DecimalConverter(object v, int scale)
        {
            var multiplier = Decimal128.GetScaleMultiplier(scale);

            return((*(Decimal128 *)((FixedLenByteArray)v).Pointer).ToDecimal(multiplier));
        }
 public override void WriteDecimal128(Decimal128 value)
 {
     Position += 16;
 }
 /// <inheritdoc />
 public override bool ToBoolean()
 {
     return(!(Decimal128.IsNaN(_value) || _value.Equals(Decimal128.Zero)));
 }
 /// <inheritdoc />
 public abstract void WriteDecimal128(Decimal128 value);
 /// <inheritdoc />
 public override double ToDouble()
 {
     return(Decimal128.ToDouble(_value));
 }
        public void Byte_overflow(int value)
        {
            var subject = new Decimal128(value);

            Action act = () => Decimal128.ToByte(subject);
            act.ShouldThrow<OverflowException>();
        }
 /// <inheritdoc />
 public override long ToInt64()
 {
     return(Decimal128.ToInt64(_value));
 }
 /// <inheritdoc/>
 public override void WriteDecimal128(Decimal128 value)
 {
     ThrowIfDisposed();
     WriteInt64((long)value.GetIEEELowBits());
     WriteInt64((long)value.GetIEEEHighBits());
 }
        public void UInt64(ulong value, string s)
        {
            var subject = new Decimal128(value);

            subject.ToString().Should().Be(s);
            AssertSpecialProperties(subject);

            var result = Decimal128.ToUInt64(subject);
            result.Should().Be(value);

            result = (ulong)subject;
            result.Should().Be(value);
        }
        public void UInt64_overflow(long value)
        {
            var subject = new Decimal128(value);

            Action act = () => Decimal128.ToUInt64(subject);
            act.ShouldThrow<OverflowException>();
        }
Exemple #55
0
 /// <inheritdoc/>
 public override void WriteDecimal128(Decimal128 value)
 {
     ThrowIfDisposed();
     WriteInt64((long)value.GetIEEELowBits());
     WriteInt64((long)value.GetIEEEHighBits());
 }
        /// <inheritdoc />
        public override void WriteDecimal128(Decimal128 value)
        {
            if (Disposed) { throw new ObjectDisposedException("JsonWriter"); }
            if (State != BsonWriterState.Value && State != BsonWriterState.Initial)
            {
                ThrowInvalidState(nameof(WriteDecimal128), BsonWriterState.Value, BsonWriterState.Initial);
            }

            WriteNameHelper(Name);
            switch (_jsonWriterSettings.OutputMode)
            {
                case JsonOutputMode.Shell:
                    _textWriter.Write("NumberDecimal(\"{0}\")", value.ToString());
                    break;

                default:
                    _textWriter.Write("{{ \"$numberDecimal\" : \"{0}\" }}", value.ToString());
                    break;
            }

            State = GetNextState();
        }