コード例 #1
0
        public void MultiplyBigDecimal()
        {
            BigDecimal multi1 = new BigDecimal(value, 5);
            BigDecimal multi2 = new BigDecimal(2.345D);
            BigDecimal result = multi1.Multiply(multi2);

            Assert.IsTrue(result.ToString().StartsWith("289.51154260") && result.Scale == multi1.Scale + multi2.Scale,
                          "123.45908 * 2.345 is not correct: " + result);
            multi1 = BigDecimal.Parse("34656");
            multi2 = BigDecimal.Parse("-2");
            result = multi1.Multiply(multi2);
            Assert.IsTrue(result.ToString().Equals("-69312") && result.Scale == 0, "34656 * 2 is not correct");
            multi1 = new BigDecimal(-2.345E-02);
            multi2 = new BigDecimal(-134E130);
            result = multi1.Multiply(multi2);
            Assert.IsTrue(result.ToDouble() == 3.1422999999999997E130 && result.Scale == multi1.Scale + multi2.Scale,
                          "-2.345E-02 * -134E130 is not correct " + result.ToDouble());
            multi1 = BigDecimal.Parse("11235");
            multi2 = BigDecimal.Parse("0");
            result = multi1.Multiply(multi2);
            Assert.IsTrue(result.ToDouble() == 0 && result.Scale == 0, "11235 * 0 is not correct");
            multi1 = BigDecimal.Parse("-0.00234");
            multi2 = new BigDecimal(13.4E10);
            result = multi1.Multiply(multi2);
            Assert.IsTrue(result.ToDouble() == -313560000 && result.Scale == multi1.Scale + multi2.Scale,
                          "-0.00234 * 13.4E10 is not correct");
        }
コード例 #2
0
        public void Abs()
        {
            BigDecimal big    = BigDecimal.Parse("-1234");
            BigDecimal bigabs = big.Abs();

            Assert.IsTrue(bigabs.ToString().Equals("1234"), "the absolute value of -1234 is not 1234");
            big    = new BigDecimal(BigInteger.Parse("2345"), 2);
            bigabs = big.Abs();
            Assert.IsTrue(bigabs.ToString().Equals("23.45"), "the absolute value of 23.45 is not 23.45");
        }
コード例 #3
0
        public void ParseString()
        {
            BigDecimal big = BigDecimal.Parse("345.23499600293850");

            Assert.IsTrue(big.ToString().Equals("345.23499600293850") && big.Scale == 14,
                          "the BigDecimal value is not initialized properly");
            big = BigDecimal.Parse("-12345");
            Assert.IsTrue(big.ToString().Equals("-12345") && big.Scale == 0, "the BigDecimal value is not initialized properly");
            big = BigDecimal.Parse("123.");
            Assert.IsTrue(big.ToString().Equals("123") && big.Scale == 0, "the BigDecimal value is not initialized properly");

            BigDecimal.Parse("1.234E02");
        }
コード例 #4
0
        public void TestToString()
        {
            BigDecimal toString1 = BigDecimal.Parse("1234.000");

            Assert.IsTrue(toString1.ToString().Equals("1234.000"), "the ToString representation of 1234.000 is wrong");
            toString1 = BigDecimal.Parse("-123.4E-5");
            Assert.IsTrue(toString1.ToString().Equals("-0.001234"),
                          "the ToString representation of -123.4E-5 is wrong: " + toString1);
            toString1 = BigDecimal.Parse("-1.455E-20");
            Assert.IsTrue(toString1.ToString().Equals("-1.455E-20"), "the ToString representation of -1.455E-20 is wrong");
            toString1 = new BigDecimal(value2, 4);
            Assert.IsTrue(toString1.ToString().Equals("1233456.0000"), "the ToString representation of 1233456.0000 is wrong");
        }
コード例 #5
0
        public void DivideBigDecimalII()
        {
            BigDecimal divd1 = new BigDecimal(value2, 4);
            BigDecimal divd2 = BigDecimal.Parse("0.0023");
            BigDecimal divd3 = divd1.Divide(divd2, 3, RoundingMode.HalfUp);

            Assert.IsTrue(divd3.ToString().Equals("536285217.391") && divd3.Scale == 3, "1233456/0.0023 is not correct");
            divd2 = new BigDecimal(1345.5E-02D);
            divd3 = divd1.Divide(divd2, 0, RoundingMode.Down);
            Assert.IsTrue(divd3.ToString().Equals("91672") && divd3.Scale == 0,
                          "1233456/13.455 is not correct or does not have the correct scale");
            divd2 = new BigDecimal(0000D);

            Assert.Throws <ArithmeticException>(() => divd1.Divide(divd2, 4, RoundingMode.Down));
        }
コード例 #6
0
        public void ConstructorBigIntegerScale()
        {
            BigDecimal big = new BigDecimal(value2, 5);

            Assert.IsTrue(big.UnscaledValue.Equals(value2) && big.Scale == 5, "the BigDecimal value is not initialized properly");
            Assert.IsTrue(big.ToString().Equals("123345.60000"), "the BigDecimal value is not represented properly");
        }
コード例 #7
0
        public void DivideBigDecimalI()
        {
            BigDecimal divd1 = new BigDecimal(value, 2);
            BigDecimal divd2 = BigDecimal.Parse("2.335");
            BigDecimal divd3 = divd1.Divide(divd2, RoundingMode.Up);

            Assert.IsTrue(divd3.ToString().Equals("52873.27") && divd3.Scale == divd1.Scale, "123459.08/2.335 is not correct");
            Assert.IsTrue(divd3.UnscaledValue.ToString().Equals("5287327"),
                          "the unscaledValue representation of 123459.08/2.335 is not correct");
            divd2 = new BigDecimal(123.4D);
            divd3 = divd1.Divide(divd2, RoundingMode.Down);
            Assert.IsTrue(divd3.ToString().Equals("1000.47") && divd3.Scale == 2, "123459.08/123.4  is not correct");
            divd2 = new BigDecimal(000D);

            Assert.Throws <ArithmeticException>(() => divd1.Divide(divd2, RoundingMode.Down));
        }
コード例 #8
0
        public void ValueOfJI()
        {
            BigDecimal valueOfJI = BigDecimal.ValueOf(9223372036854775806L, 5);

            Assert.IsTrue(valueOfJI.UnscaledValue.ToString().Equals("9223372036854775806") && valueOfJI.Scale == 5,
                          "the bigDecimal equivalent of 92233720368547.75806 is wrong");
            Assert.IsTrue(valueOfJI.ToString().Equals("92233720368547.75806"),
                          "the ToString representation of 9223372036854775806 is wrong");
            valueOfJI = BigDecimal.ValueOf(1234L, 8);
            Assert.IsTrue(valueOfJI.UnscaledValue.ToString().Equals("1234") && valueOfJI.Scale == 8,
                          "the bigDecimal equivalent of 92233720368547.75806 is wrong");
            Assert.IsTrue(valueOfJI.ToString().Equals("0.00001234"),
                          "the ToString representation of 9223372036854775806 is wrong");
            valueOfJI = BigDecimal.ValueOf(0, 3);
            Assert.IsTrue(valueOfJI.UnscaledValue.ToString().Equals("0") && valueOfJI.Scale == 3,
                          "the bigDecimal equivalent of 92233720368547.75806 is wrong");
            Assert.IsTrue(valueOfJI.ToString().Equals("0.000"), "the ToString representation of 9223372036854775806 is wrong");
        }
コード例 #9
0
        public void ValueOfJ()
        {
            BigDecimal valueOfL = BigDecimal.ValueOf(9223372036854775806L);

            Assert.IsTrue(valueOfL.UnscaledValue.ToString().Equals("9223372036854775806") && valueOfL.Scale == 0,
                          "the bigDecimal equivalent of 9223372036854775806 is wrong");
            Assert.IsTrue(valueOfL.ToString().Equals("9223372036854775806"),
                          "the ToString representation of 9223372036854775806 is wrong");
            valueOfL = BigDecimal.ValueOf(0L);
            Assert.IsTrue(valueOfL.UnscaledValue.ToString().Equals("0") && valueOfL.Scale == 0,
                          "the bigDecimal equivalent of 0 is wrong");
        }
コード例 #10
0
        public void MovePointRightI()
        {
            BigDecimal movePtRight  = BigDecimal.Parse("-1.58796521458");
            BigDecimal alreadyMoved = movePtRight.MovePointRight(8);

            Assert.IsTrue(alreadyMoved.Scale == 3 && alreadyMoved.ToString().Equals("-158796521.458"),
                          "move point right 8 failed");
            movePtRight  = new BigDecimal(value, 2);
            alreadyMoved = movePtRight.MovePointRight(4);
            Assert.IsTrue(alreadyMoved.Scale == 0 && alreadyMoved.ToString().Equals("1234590800"), "move point right 4 failed");
            movePtRight  = new BigDecimal(134E12);
            alreadyMoved = movePtRight.MovePointRight(2);
            Assert.IsTrue(alreadyMoved.Scale == 0 && alreadyMoved.ToString().Equals("13400000000000000"),
                          "move point right 2 failed");
            movePtRight  = new BigDecimal(-3.4E-10);
            alreadyMoved = movePtRight.MovePointRight(5);
            Assert.IsTrue(alreadyMoved.Scale == movePtRight.Scale - 5 && alreadyMoved.ToDouble() == -0.000034,
                          "move point right 5 failed");
            alreadyMoved = alreadyMoved.MovePointRight(-5);
            Assert.IsTrue(alreadyMoved.Equals(movePtRight), "move point right -5 failed");
        }
コード例 #11
0
        public void AddBigDecimal()
        {
            BigDecimal add1 = BigDecimal.Parse("23.456");
            BigDecimal add2 = BigDecimal.Parse("3849.235");
            BigDecimal sum  = add1.Add(add2);

            Assert.IsTrue(sum.UnscaledValue.ToString().Equals("3872691") && sum.Scale == 3,
                          "the sum of 23.456 + 3849.235 is wrong");
            Assert.IsTrue(sum.ToString().Equals("3872.691"), "the sum of 23.456 + 3849.235 is not printed correctly");
            BigDecimal add3 = new BigDecimal(12.34E02D);

            Assert.IsTrue((add1.Add(add3)).ToString().Equals("1257.456"), "the sum of 23.456 + 12.34E02 is not printed correctly");
        }
コード例 #12
0
        public void MovePointLeftI()
        {
            BigDecimal movePtLeft   = BigDecimal.Parse("123456265.34");
            BigDecimal alreadyMoved = movePtLeft.MovePointLeft(5);

            Assert.IsTrue(alreadyMoved.Scale == 7 && alreadyMoved.ToString().Equals("1234.5626534"), "move point left 5 failed");
            movePtLeft   = new BigDecimal(value2.Negate(), 0);
            alreadyMoved = movePtLeft.MovePointLeft(12);
            Assert.IsTrue(alreadyMoved.Scale == 12 && alreadyMoved.ToString().Equals("-0.012334560000"),
                          "move point left 12 failed");
            movePtLeft   = new BigDecimal(123E18);
            alreadyMoved = movePtLeft.MovePointLeft(2);
            Assert.IsTrue(alreadyMoved.Scale == movePtLeft.Scale + 2 && alreadyMoved.ToDouble() == 1.23E18,
                          "move point left 2 failed");
            movePtLeft   = new BigDecimal(1.123E-12);
            alreadyMoved = movePtLeft.MovePointLeft(3);
            Assert.IsTrue(alreadyMoved.Scale == movePtLeft.Scale + 3 && alreadyMoved.ToDouble() == 1.123E-15,
                          "move point left 3 failed");
            movePtLeft   = new BigDecimal(value, 2);
            alreadyMoved = movePtLeft.MovePointLeft(-2);
            Assert.IsTrue(alreadyMoved.Scale == movePtLeft.Scale - 2 && alreadyMoved.ToString().Equals("12345908"),
                          "move point left -2 failed");
        }
コード例 #13
0
        public void BigDecimalSerialization()
        {
            // Regression for HARMONY-1896
            char[]     input = { '1', '5', '6', '7', '8', '7', '.', '0', '0' };
            BigDecimal bd    = BigDecimal.Parse(input, 0, 9);

            MemoryStream    bos = new MemoryStream();
            BinaryFormatter oos = new BinaryFormatter();

            oos.Serialize(bos, bd);

            MemoryStream bis = new MemoryStream(bos.ToArray());
            BigDecimal   nbd = (BigDecimal)oos.Deserialize(bis);

            Assert.AreEqual(bd.ToInt32(), nbd.ToInt32());
            Assert.AreEqual(bd.ToDouble(), nbd.ToDouble(), 0.0);
            Assert.AreEqual(bd.ToString(), nbd.ToString());
        }
コード例 #14
0
        public void ConstructorDouble()
        {
            BigDecimal big = new BigDecimal(123E04);

            Assert.Equal("1230000", big.ToString());
            big = new BigDecimal(1.2345E-12);
            Assert.Equal(1.2345E-12, big.ToDouble());
            big = new BigDecimal(-12345E-3);
            Assert.Equal(-12.345, big.ToDouble());
            big = new BigDecimal(5.1234567897654321e138);
            Assert.Equal(5.1234567897654321E138, big.ToDouble());
            Assert.Equal(0, big.Scale);
            big = new BigDecimal(0.1);
            Assert.True(big.ToDouble() == 0.1, "the double representation of 0.1 bigDecimal is not correct");
            big = new BigDecimal(0.00345);
            Assert.True(big.ToDouble() == 0.00345, "the double representation of 0.00345 bigDecimal is not correct");
            // regression test for HARMONY-2429
            big = new BigDecimal(-0.0);
            Assert.True(big.Scale == 0, "the double representation of -0.0 bigDecimal is not correct");
        }
コード例 #15
0
        public void ConstructorDouble()
        {
            BigDecimal big = new BigDecimal(123E04);

            Assert.AreEqual("1230000", big.ToString(),
                            "the BigDecimal value taking a double argument is not initialized properly");
            big = new BigDecimal(1.2345E-12);
            Assert.AreEqual(1.2345E-12, big.ToDouble(), "the double representation is not correct");
            big = new BigDecimal(-12345E-3);
            Assert.AreEqual(-12.345, big.ToDouble(), "the double representation is not correct");
            big = new BigDecimal(5.1234567897654321e138);
            Assert.AreEqual(5.1234567897654321E138, big.ToDouble(), "the double representation is not correct");
            Assert.AreEqual(0, big.Scale, "the double representation is not correct");
            big = new BigDecimal(0.1);
            Assert.IsTrue(big.ToDouble() == 0.1, "the double representation of 0.1 bigDecimal is not correct");
            big = new BigDecimal(0.00345);
            Assert.IsTrue(big.ToDouble() == 0.00345, "the double representation of 0.00345 bigDecimal is not correct");
            // regression test for HARMONY-2429
            big = new BigDecimal(-0.0);
            Assert.IsTrue(big.Scale == 0, "the double representation of -0.0 bigDecimal is not correct");
        }
コード例 #16
0
        public void SubtractBigDecimal()
        {
            BigDecimal sub1   = BigDecimal.Parse("13948");
            BigDecimal sub2   = BigDecimal.Parse("2839.489");
            BigDecimal result = sub1.Subtract(sub2);

            Assert.IsTrue(result.ToString().Equals("11108.511") && result.Scale == 3, "13948 - 2839.489 is wrong: " + result);
            BigDecimal result2 = sub2.Subtract(sub1);

            Assert.IsTrue(result2.ToString().Equals("-11108.511") && result2.Scale == 3, "2839.489 - 13948 is wrong");
            Assert.IsTrue(result.Equals(result2.Negate()), "13948 - 2839.489 is not the negative of 2839.489 - 13948");
            sub1   = new BigDecimal(value, 1);
            sub2   = BigDecimal.Parse("0");
            result = sub1.Subtract(sub2);
            Assert.IsTrue(result.Equals(sub1), "1234590.8 - 0 is wrong");
            sub1   = new BigDecimal(1.234E-03);
            sub2   = new BigDecimal(3.423E-10);
            result = sub1.Subtract(sub2);
            Assert.IsTrue(result.ToDouble() == 0.0012339996577, "1.234E-03 - 3.423E-10 is wrong, " + result.ToDouble());
            sub1   = new BigDecimal(1234.0123);
            sub2   = new BigDecimal(1234.0123000);
            result = sub1.Subtract(sub2);
            Assert.IsTrue(result.ToDouble() == 0.0, "1234.0123 - 1234.0123000 is wrong, " + result.ToDouble());
        }
コード例 #17
0
        public void SetScaleII()
        {
            BigDecimal setScale1 = new BigDecimal(2.323E102);
            BigDecimal setScale2 = setScale1.SetScale(4);

            Assert.IsTrue(setScale2.Scale == 4, "the number 2.323E102 after setting scale is wrong");
            Assert.IsTrue(setScale2.ToDouble() == 2.323E102, "the representation of the number 2.323E102 is wrong");
            setScale1 = BigDecimal.Parse("-1.253E-12");
            setScale2 = setScale1.SetScale(17, RoundingMode.Ceiling);
            Assert.IsTrue(setScale2.Scale == 17, "the number -1.253E-12 after setting scale is wrong");
            Assert.IsTrue(setScale2.ToString().Equals("-1.25300E-12"),
                          "the representation of the number -1.253E-12 after setting scale is wrong, " + setScale2);

            // testing rounding Mode ROUND_CEILING
            setScale1 = new BigDecimal(value, 4);
            setScale2 = setScale1.SetScale(1, RoundingMode.Ceiling);
            Assert.IsTrue(setScale2.ToString().Equals("1234.6") && setScale2.Scale == 1,
                          "the number 1234.5908 after setting scale to 1/ROUND_CEILING is wrong");
            BigDecimal setNeg = new BigDecimal(value.Negate(), 4);

            setScale2 = setNeg.SetScale(1, RoundingMode.Ceiling);
            Assert.IsTrue(setScale2.ToString().Equals("-1234.5") && setScale2.Scale == 1,
                          "the number -1234.5908 after setting scale to 1/ROUND_CEILING is wrong");

            // testing rounding Mode ROUND_DOWN
            setScale2 = setNeg.SetScale(1, RoundingMode.Down);
            Assert.IsTrue(setScale2.ToString().Equals("-1234.5") && setScale2.Scale == 1,
                          "the number -1234.5908 after setting scale to 1/ROUND_DOWN is wrong");
            setScale1 = new BigDecimal(value, 4);
            setScale2 = setScale1.SetScale(1, RoundingMode.Down);
            Assert.IsTrue(setScale2.ToString().Equals("1234.5") && setScale2.Scale == 1,
                          "the number 1234.5908 after setting scale to 1/ROUND_DOWN is wrong");

            // testing rounding Mode ROUND_FLOOR
            setScale2 = setScale1.SetScale(1, RoundingMode.Floor);
            Assert.IsTrue(setScale2.ToString().Equals("1234.5") && setScale2.Scale == 1,
                          "the number 1234.5908 after setting scale to 1/ROUND_FLOOR is wrong");
            setScale2 = setNeg.SetScale(1, RoundingMode.Floor);
            Assert.IsTrue(setScale2.ToString().Equals("-1234.6") && setScale2.Scale == 1,
                          "the number -1234.5908 after setting scale to 1/ROUND_FLOOR is wrong");

            // testing rounding Mode ROUND_HALF_DOWN
            setScale2 = setScale1.SetScale(3, RoundingMode.HalfDown);
            Assert.IsTrue(setScale2.ToString().Equals("1234.591") && setScale2.Scale == 3,
                          "the number 1234.5908 after setting scale to 3/ROUND_HALF_DOWN is wrong");
            setScale1 = new BigDecimal(BigInteger.Parse("12345000"), 5);
            setScale2 = setScale1.SetScale(1, RoundingMode.HalfDown);
            Assert.IsTrue(setScale2.ToString().Equals("123.4") && setScale2.Scale == 1,
                          "the number 123.45908 after setting scale to 1/ROUND_HALF_DOWN is wrong");
            setScale2 = BigDecimal.Parse("-1234.5000").SetScale(0, RoundingMode.HalfDown);
            Assert.IsTrue(setScale2.ToString().Equals("-1234") && setScale2.Scale == 0,
                          "the number -1234.5908 after setting scale to 0/ROUND_HALF_DOWN is wrong");

            // testing rounding Mode ROUND_HALF_EVEN
            setScale1 = new BigDecimal(1.2345789D);
            setScale2 = setScale1.SetScale(4, RoundingMode.HalfEven);
            Assert.IsTrue(setScale2.ToDouble() == 1.2346D && setScale2.Scale == 4,
                          "the number 1.2345789 after setting scale to 4/ROUND_HALF_EVEN is wrong");
            setNeg    = new BigDecimal(-1.2335789D);
            setScale2 = setNeg.SetScale(2, RoundingMode.HalfEven);
            Assert.IsTrue(setScale2.ToDouble() == -1.23D && setScale2.Scale == 2,
                          "the number -1.2335789 after setting scale to 2/ROUND_HALF_EVEN is wrong");
            setScale2 = BigDecimal.Parse("1.2345000").SetScale(3,
                                                               RoundingMode.HalfEven);
            Assert.IsTrue(setScale2.ToDouble() == 1.234D && setScale2.Scale == 3,
                          "the number 1.2345789 after setting scale to 3/ROUND_HALF_EVEN is wrong");
            setScale2 = BigDecimal.Parse("-1.2345000").SetScale(3,
                                                                RoundingMode.HalfEven);
            Assert.IsTrue(setScale2.ToDouble() == -1.234D && setScale2.Scale == 3,
                          "the number -1.2335789 after setting scale to 3/ROUND_HALF_EVEN is wrong");

            // testing rounding Mode ROUND_HALF_UP
            setScale1 = BigDecimal.Parse("134567.34650");
            setScale2 = setScale1.SetScale(3, RoundingMode.HalfUp);
            Assert.IsTrue(setScale2.ToString().Equals("134567.347") && setScale2.Scale == 3,
                          "the number 134567.34658 after setting scale to 3/ROUND_HALF_UP is wrong");
            setNeg    = BigDecimal.Parse("-1234.4567");
            setScale2 = setNeg.SetScale(0, RoundingMode.HalfUp);
            Assert.IsTrue(setScale2.ToString().Equals("-1234") && setScale2.Scale == 0,
                          "the number -1234.4567 after setting scale to 0/ROUND_HALF_UP is wrong");

            Assert.Throws <ArithmeticException>(() => setScale1.SetScale(3, RoundingMode.Unnecessary));

            // testing rounding Mode ROUND_UP
            setScale1 = BigDecimal.Parse("100000.374");
            setScale2 = setScale1.SetScale(2, RoundingMode.Up);
            Assert.IsTrue(setScale2.ToString().Equals("100000.38") && setScale2.Scale == 2,
                          "the number 100000.374 after setting scale to 2/ROUND_UP is wrong");
            setNeg    = new BigDecimal(-134.34589D);
            setScale2 = setNeg.SetScale(2, RoundingMode.Up);
            Assert.IsTrue(setScale2.ToDouble() == -134.35D && setScale2.Scale == 2,
                          "the number -134.34589 after setting scale to 2/ROUND_UP is wrong");

            Assert.Throws <ArgumentException>(() => setScale1.SetScale(0, -123));
        }