1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
|
// Test of setScale method of BigDecimal. (PR 1596)
/*************************************************************************
/* This program is free software; you can redistribute it and/or modify
/* it under the terms of the GNU General Public License as published
/* by the Free Software Foundation, either version 2 of the License, or
/* (at your option) any later version.
/*
/* This program is distributed in the hope that it will be useful, but
/* WITHOUT ANY WARRANTY; without even the implied warranty of
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
/* GNU General Public License for more details.
/*
/* You should have received a copy of the GNU General Public License
/* along with this program; if not, write to the Free Software Foundation
/* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307 USA
/*************************************************************************/
// Tags: JDK1.1
package gnu.testlet.java.math.BigDecimal;
import java.math.BigDecimal;
import gnu.testlet.Testlet;
import gnu.testlet.TestHarness;
public class setScale implements Testlet
{
public void test (TestHarness harness)
{
harness.checkPoint ("newScale");
BigDecimal amount = new BigDecimal ("12.34");
BigDecimal rate = new BigDecimal ("0.06");
BigDecimal result = amount.multiply (rate);
try
{
BigDecimal foo = result.setScale(-1, BigDecimal.ROUND_UNNECESSARY);
harness.fail ("Failed to catch ArithmeticException");
}
catch (ArithmeticException e)
{
harness.check (true);
}
harness.check (result.setScale(0, BigDecimal.ROUND_HALF_UP),
new BigDecimal ("1"));
harness.check (result.setScale(1, BigDecimal.ROUND_HALF_UP),
new BigDecimal ("0.7"));
harness.check (result.setScale(2, BigDecimal.ROUND_HALF_UP),
new BigDecimal ("0.74"));
harness.check (result.setScale(3, BigDecimal.ROUND_HALF_UP),
new BigDecimal ("0.740"));
harness.check (result.setScale(4, BigDecimal.ROUND_HALF_UP),
new BigDecimal ("0.7404"));
// setScale testcase from Jerry Quinn <jlquinn@optonline.net>
harness.checkPoint ("quinn");
BigDecimal x = new BigDecimal("0.20562");
harness.check (x.toString(), "0.20562");
x = x.setScale(2, BigDecimal.ROUND_HALF_EVEN); // to x.xx
harness.check (x.toString(), "0.21");
x = new BigDecimal("0.20499");
x = x.setScale(2, BigDecimal.ROUND_HALF_EVEN);
harness.check (x.toString(), "0.20");
x = new BigDecimal("0.20500");
x = x.setScale(2, BigDecimal.ROUND_HALF_EVEN);
harness.check (x.toString(), "0.20");
x = new BigDecimal("0.20501");
x = x.setScale(2, BigDecimal.ROUND_HALF_EVEN);
harness.check (x.toString(), "0.21");
x = new BigDecimal("0.21499");
x = x.setScale(2, BigDecimal.ROUND_HALF_EVEN);
harness.check (x.toString(), "0.21");
x = new BigDecimal("0.21500");
x = x.setScale(2, BigDecimal.ROUND_HALF_EVEN);
harness.check (x.toString(), "0.22");
x = new BigDecimal("0.21501");
x = x.setScale(2, BigDecimal.ROUND_HALF_EVEN);
harness.check (x.toString(), "0.22");
// And now the negative versions.
harness.checkPoint ("quinneg");
x = new BigDecimal("-0.20562");
harness.verbose(x.toString());
x = x.setScale(2, BigDecimal.ROUND_HALF_EVEN); // to x.xx
harness.check (x.toString(), "-0.21");
x = new BigDecimal("-0.20499");
harness.verbose(x.toString());
x = x.setScale(2, BigDecimal.ROUND_HALF_EVEN);
harness.check (x.toString(), "-0.20");
x = new BigDecimal("-0.20500");
harness.verbose(x.toString());
x = x.setScale(2, BigDecimal.ROUND_HALF_EVEN);
harness.check (x.toString(), "-0.20");
x = new BigDecimal("-0.20501");
harness.verbose(x.toString());
x = x.setScale(2, BigDecimal.ROUND_HALF_EVEN);
harness.check (x.toString(), "-0.21");
x = new BigDecimal("-0.21499");
harness.verbose(x.toString());
x = x.setScale(2, BigDecimal.ROUND_HALF_EVEN);
harness.check (x.toString(), "-0.21");
x = new BigDecimal("-0.21500");
harness.verbose(x.toString());
x = x.setScale(2, BigDecimal.ROUND_HALF_EVEN);
harness.check (x.toString(), "-0.22");
x = new BigDecimal("-0.21501");
harness.verbose(x.toString());
x = x.setScale(2, BigDecimal.ROUND_HALF_EVEN);
harness.check (x.toString(), "-0.22");
}
}
|