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"); }
public void ToDouble() { BigDecimal bigDB = new BigDecimal(-1.234E-112); // Commenting out this part because it causes an endless loop (see HARMONY-319 and HARMONY-329) // Assert.IsTrue( // "the double representation of this BigDecimal is not correct", // bigDB.ToDouble() == -1.234E-112); bigDB = new BigDecimal(5.00E-324); Assert.IsTrue(bigDB.ToDouble() == 5.00E-324, "the double representation of bigDecimal is not correct"); bigDB = new BigDecimal(1.79E308); Assert.IsTrue(bigDB.ToDouble() == 1.79E308 && bigDB.Scale == 0, "the double representation of bigDecimal is not correct"); bigDB = new BigDecimal(-2.33E102); Assert.IsTrue(bigDB.ToDouble() == -2.33E102 && bigDB.Scale == 0, "the double representation of bigDecimal -2.33E102 is not correct"); bigDB = new BigDecimal(Double.MaxValue); bigDB = bigDB.Add(bigDB); Assert.IsTrue(bigDB.ToDouble() == Double.PositiveInfinity, "a + number out of the double range should return infinity"); bigDB = new BigDecimal(-Double.MaxValue); bigDB = bigDB.Add(bigDB); Assert.IsTrue(bigDB.ToDouble() == Double.NegativeInfinity, "a - number out of the double range should return neg infinity"); }
public double ToDouble() { /* To meet the risk of individual overflows of the exponents of * a separate invocation a.doubleValue() or Denominator.doubleValue(), we divide first * in a BigDecimal environment and convert the result. */ BigDecimal adivb = (new BigDecimal(Numerator)).Divide(new BigDecimal(Denominator), MathContext.Decimal128); return(adivb.ToDouble()); }
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"); }
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()); }
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()); }
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"); }
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"); }
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"); }
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)); }