public void SetScaleI() { // rounding mode defaults to zero BigDecimal setScale1 = new BigDecimal(value, 3); BigDecimal setScale2 = setScale1.SetScale(5); BigInteger setresult = BigInteger.Parse("1234590800"); Assert.IsTrue(setScale2.UnscaledValue.Equals(setresult) && setScale2.Scale == 5, "the number 12345.908 after setting scale is wrong"); Assert.Throws <ArithmeticException>(() => setScale1.SetScale(2, RoundingMode.Unnecessary)); }
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)); }