01: /*
02: * Copyright 2005-2007 The Kuali Foundation.
03: *
04: * Licensed under the Educational Community License, Version 1.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.opensource.org/licenses/ecl1.php
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16: package org.kuali.bo;
17:
18: import java.math.BigDecimal;
19:
20: import org.kuali.kfs.context.KualiTestBase;
21:
22: /**
23: * This class is used to test BigDecimal.
24: */
25: public class BigDecimalTest extends KualiTestBase {
26:
27: private void testMultiply(String val1, String val2, String result) {
28:
29: BigDecimal a = new BigDecimal(val1);
30: BigDecimal b = new BigDecimal(val2);
31:
32: BigDecimal c = a.multiply(b);
33:
34: assertEquals("Result should be " + result + ".", result, c
35: .toString());
36:
37: }
38:
39: private void testAdd(String val1, String val2, String result) {
40:
41: BigDecimal a = new BigDecimal(val1);
42: BigDecimal b = new BigDecimal(val2);
43:
44: BigDecimal c = a.add(b);
45:
46: assertEquals("Result should be " + result + ".", result, c
47: .toString());
48:
49: }
50:
51: private void testRounding(String val1, int scale, int roundMethod,
52: String result) {
53:
54: BigDecimal a = new BigDecimal(val1);
55: BigDecimal b;
56:
57: b = a.setScale(scale, roundMethod);
58: assertEquals("Result should be " + result + ".", result, b
59: .toString());
60:
61: }
62:
63: public void testMultipleOperations() {
64:
65: // TEST: 100.222 * 100.222 = 10044.449284 - should retain 6 digits of precision
66: testMultiply("100.222", "100.222", "10044.449284");
67:
68: // TEST: 0.88 * 0.22 = 0.1936
69: testMultiply("0.88", "0.22", "0.1936");
70:
71: // TEST: 1.55555 + 1.22 = 2.77555 - should retain 5 digits of precision
72: testAdd("1.55555", "1.22", "2.77555");
73:
74: // TEST: Banker's Round(2) - 21.855 = 21.86
75: testRounding("21.855", 2, BigDecimal.ROUND_HALF_EVEN, "21.86");
76:
77: // TEST: Banker's Round(2) - 21.865 = 21.86
78: testRounding("21.865", 2, BigDecimal.ROUND_HALF_EVEN, "21.86");
79:
80: // TEST: Banker's Round(2) - 21.875 = 21.88
81: testRounding("21.875", 2, BigDecimal.ROUND_HALF_EVEN, "21.88");
82:
83: // TEST: Banker's Round(2) - 21.875 = 21.88
84: testRounding("21.885", 2, BigDecimal.ROUND_HALF_EVEN, "21.88");
85:
86: // TEST: Arithmetic Round(2) - 21.855 = 21.86
87: testRounding("21.855", 2, BigDecimal.ROUND_HALF_UP, "21.86");
88:
89: // TEST: Banker's Round(2) - 21.865 = 21.86
90: testRounding("21.865", 2, BigDecimal.ROUND_HALF_UP, "21.87");
91:
92: // TEST: Banker's Round(2) - 21.875 = 21.88
93: testRounding("21.875", 2, BigDecimal.ROUND_HALF_UP, "21.88");
94:
95: // TEST: Banker's Round(2) - 21.875 = 21.88
96: testRounding("21.885", 2, BigDecimal.ROUND_HALF_UP, "21.89");
97: }
98:
99: }
|