001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Elena Semukhina
019: * @version $Revision$
020: */package org.apache.harmony.tests.java.math;
021:
022: import java.math.BigDecimal;
023: import java.math.BigInteger;
024: import java.math.MathContext;
025: import java.math.RoundingMode;
026:
027: import junit.framework.TestCase;
028:
029: /**
030: * Class: java.math.BigDecimal
031: * Methods: constructors and fields
032: */
033: public class BigDecimalConstructorsTest extends TestCase {
034: /**
035: * check ONE
036: */
037: public void testFieldONE() {
038: String oneS = "1";
039: double oneD = 1.0;
040: assertEquals("incorrect string value", oneS, BigDecimal.ONE
041: .toString());
042: assertEquals("incorrect double value", oneD, BigDecimal.ONE
043: .doubleValue(), 0);
044: }
045:
046: /**
047: * check TEN
048: */
049: public void testFieldTEN() {
050: String oneS = "10";
051: double oneD = 10.0;
052: assertEquals("incorrect string value", oneS, BigDecimal.TEN
053: .toString());
054: assertEquals("incorrect double value", oneD, BigDecimal.TEN
055: .doubleValue(), 0);
056: }
057:
058: /**
059: * check ZERO
060: */
061: public void testFieldZERO() {
062: String oneS = "0";
063: double oneD = 0.0;
064: assertEquals("incorrect string value", oneS, BigDecimal.ZERO
065: .toString());
066: assertEquals("incorrect double value", oneD, BigDecimal.ZERO
067: .doubleValue(), 0);
068: }
069:
070: /**
071: * new BigDecimal(BigInteger value)
072: */
073: public void testConstrBI() {
074: String a = "1231212478987482988429808779810457634781384756794987";
075: BigInteger bA = new BigInteger(a);
076: BigDecimal aNumber = new BigDecimal(bA);
077: assertEquals("incorrect value", bA, aNumber.unscaledValue());
078: assertEquals("incorrect scale", 0, aNumber.scale());
079:
080: try {
081: new BigDecimal((BigInteger) null);
082: fail("No NullPointerException");
083: } catch (NullPointerException e) {
084: //expected
085: }
086: }
087:
088: /**
089: * new BigDecimal(BigInteger value, int scale)
090: */
091: public void testConstrBIScale() {
092: String a = "1231212478987482988429808779810457634781384756794987";
093: BigInteger bA = new BigInteger(a);
094: int aScale = 10;
095: BigDecimal aNumber = new BigDecimal(bA, aScale);
096: assertEquals("incorrect value", bA, aNumber.unscaledValue());
097: assertEquals("incorrect scale", aScale, aNumber.scale());
098: }
099:
100: /**
101: * new BigDecimal(BigInteger value, MathContext)
102: */
103: public void testConstrBigIntegerMathContext() {
104: String a = "1231212478987482988429808779810457634781384756794987";
105: BigInteger bA = new BigInteger(a);
106: int precision = 46;
107: RoundingMode rm = RoundingMode.CEILING;
108: MathContext mc = new MathContext(precision, rm);
109: String res = "1231212478987482988429808779810457634781384757";
110: int resScale = -6;
111: BigDecimal result = new BigDecimal(bA, mc);
112: assertEquals("incorrect value", res, result.unscaledValue()
113: .toString());
114: assertEquals("incorrect scale", resScale, result.scale());
115: }
116:
117: /**
118: * new BigDecimal(BigInteger value, int scale, MathContext)
119: */
120: public void testConstrBigIntegerScaleMathContext() {
121: String a = "1231212478987482988429808779810457634781384756794987";
122: BigInteger bA = new BigInteger(a);
123: int aScale = 10;
124: int precision = 46;
125: RoundingMode rm = RoundingMode.CEILING;
126: MathContext mc = new MathContext(precision, rm);
127: String res = "1231212478987482988429808779810457634781384757";
128: int resScale = 4;
129: BigDecimal result = new BigDecimal(bA, aScale, mc);
130: assertEquals("incorrect value", res, result.unscaledValue()
131: .toString());
132: assertEquals("incorrect scale", resScale, result.scale());
133: }
134:
135: /**
136: * new BigDecimal(char[] value);
137: */
138: public void testConstrChar() {
139: char value[] = { '-', '1', '2', '3', '8', '0', '.', '4', '7',
140: '3', '8', 'E', '-', '4', '2', '3' };
141: BigDecimal result = new BigDecimal(value);
142: String res = "-1.23804738E-419";
143: int resScale = 427;
144: assertEquals("incorrect value", res, result.toString());
145: assertEquals("incorrect scale", resScale, result.scale());
146:
147: try {
148: // Regression for HARMONY-783
149: new BigDecimal(new char[] {});
150: fail("NumberFormatException has not been thrown");
151: } catch (NumberFormatException e) {
152: }
153: }
154:
155: /**
156: * new BigDecimal(char[] value, int offset, int len);
157: */
158: public void testConstrCharIntInt() {
159: char value[] = { '-', '1', '2', '3', '8', '0', '.', '4', '7',
160: '3', '8', 'E', '-', '4', '2', '3' };
161: int offset = 3;
162: int len = 12;
163: BigDecimal result = new BigDecimal(value, offset, len);
164: String res = "3.804738E-40";
165: int resScale = 46;
166: assertEquals("incorrect value", res, result.toString());
167: assertEquals("incorrect scale", resScale, result.scale());
168:
169: try {
170: // Regression for HARMONY-783
171: new BigDecimal(new char[] {}, 0, 0);
172: fail("NumberFormatException has not been thrown");
173: } catch (NumberFormatException e) {
174: }
175: }
176:
177: /**
178: * new BigDecimal(char[] value, int offset, int len, MathContext mc);
179: */
180: public void testConstrCharIntIntMathContext() {
181: char value[] = { '-', '1', '2', '3', '8', '0', '.', '4', '7',
182: '3', '8', 'E', '-', '4', '2', '3' };
183: int offset = 3;
184: int len = 12;
185: int precision = 4;
186: RoundingMode rm = RoundingMode.CEILING;
187: MathContext mc = new MathContext(precision, rm);
188: BigDecimal result = new BigDecimal(value, offset, len, mc);
189: String res = "3.805E-40";
190: int resScale = 43;
191: assertEquals("incorrect value", res, result.toString());
192: assertEquals("incorrect scale", resScale, result.scale());
193:
194: try {
195: // Regression for HARMONY-783
196: new BigDecimal(new char[] {}, 0, 0, MathContext.DECIMAL32);
197: fail("NumberFormatException has not been thrown");
198: } catch (NumberFormatException e) {
199: }
200: }
201:
202: /**
203: * new BigDecimal(char[] value, int offset, int len, MathContext mc);
204: */
205: public void testConstrCharIntIntMathContextException1() {
206: char value[] = { '-', '1', '2', '3', '8', '0', '.', '4', '7',
207: '3', '8', 'E', '-', '4', '2', '3' };
208: int offset = 3;
209: int len = 120;
210: int precision = 4;
211: RoundingMode rm = RoundingMode.CEILING;
212: MathContext mc = new MathContext(precision, rm);
213: try {
214: new BigDecimal(value, offset, len, mc);
215: fail("NumberFormatException has not been thrown");
216: } catch (NumberFormatException e) {
217: }
218: }
219:
220: /**
221: * new BigDecimal(char[] value, int offset, int len, MathContext mc);
222: */
223: public void testConstrCharIntIntMathContextException2() {
224: char value[] = { '-', '1', '2', '3', '8', '0', ',', '4', '7',
225: '3', '8', 'E', '-', '4', '2', '3' };
226: int offset = 3;
227: int len = 120;
228: int precision = 4;
229: RoundingMode rm = RoundingMode.CEILING;
230: MathContext mc = new MathContext(precision, rm);
231: try {
232: new BigDecimal(value, offset, len, mc);
233: fail("NumberFormatException has not been thrown");
234: } catch (NumberFormatException e) {
235: }
236: }
237:
238: /**
239: * new BigDecimal(char[] value, MathContext mc);
240: */
241: public void testConstrCharMathContext() {
242: try {
243: // Regression for HARMONY-783
244: new BigDecimal(new char[] {}, MathContext.DECIMAL32);
245: fail("NumberFormatException has not been thrown");
246: } catch (NumberFormatException e) {
247: }
248: }
249:
250: /**
251: * new BigDecimal(double value) when value is NaN
252: */
253: public void testConstrDoubleNaN() {
254: double a = Double.NaN;
255: try {
256: new BigDecimal(a);
257: fail("NumberFormatException has not been caught");
258: } catch (NumberFormatException e) {
259: assertEquals("Improper exception message",
260: "Infinite or NaN", e.getMessage());
261: }
262: }
263:
264: /**
265: * new BigDecimal(double value) when value is positive infinity
266: */
267: public void testConstrDoublePosInfinity() {
268: double a = Double.POSITIVE_INFINITY;
269: try {
270: new BigDecimal(a);
271: fail("NumberFormatException has not been caught");
272: } catch (NumberFormatException e) {
273: assertEquals("Improper exception message",
274: "Infinite or NaN", e.getMessage());
275: }
276: }
277:
278: /**
279: * new BigDecimal(double value) when value is positive infinity
280: */
281: public void testConstrDoubleNegInfinity() {
282: double a = Double.NEGATIVE_INFINITY;
283: try {
284: new BigDecimal(a);
285: fail("NumberFormatException has not been caught");
286: } catch (NumberFormatException e) {
287: assertEquals("Improper exception message",
288: "Infinite or NaN", e.getMessage());
289: }
290: }
291:
292: /**
293: * new BigDecimal(double value)
294: */
295: public void testConstrDouble() {
296: double a = 732546982374982347892379283571094797.287346782359284756;
297: int aScale = 0;
298: BigInteger bA = new BigInteger(
299: "732546982374982285073458350476230656");
300: BigDecimal aNumber = new BigDecimal(a);
301: assertEquals("incorrect value", bA, aNumber.unscaledValue());
302: assertEquals("incorrect scale", aScale, aNumber.scale());
303: }
304:
305: /**
306: * new BigDecimal(double, MathContext)
307: */
308: public void testConstrDoubleMathContext() {
309: double a = 732546982374982347892379283571094797.287346782359284756;
310: int precision = 21;
311: RoundingMode rm = RoundingMode.CEILING;
312: MathContext mc = new MathContext(precision, rm);
313: String res = "732546982374982285074";
314: int resScale = -15;
315: BigDecimal result = new BigDecimal(a, mc);
316: assertEquals("incorrect value", res, result.unscaledValue()
317: .toString());
318: assertEquals("incorrect scale", resScale, result.scale());
319: }
320:
321: /**
322: * new BigDecimal(0.1)
323: */
324: public void testConstrDouble01() {
325: double a = 1.E-1;
326: int aScale = 55;
327: BigInteger bA = new BigInteger(
328: "1000000000000000055511151231257827021181583404541015625");
329: BigDecimal aNumber = new BigDecimal(a);
330: assertEquals("incorrect value", bA, aNumber.unscaledValue());
331: assertEquals("incorrect scale", aScale, aNumber.scale());
332: }
333:
334: /**
335: * new BigDecimal(0.555)
336: */
337: public void testConstrDouble02() {
338: double a = 0.555;
339: int aScale = 53;
340: BigInteger bA = new BigInteger(
341: "55500000000000004884981308350688777863979339599609375");
342: BigDecimal aNumber = new BigDecimal(a);
343: assertEquals("incorrect value", bA, aNumber.unscaledValue());
344: assertEquals("incorrect scale", aScale, aNumber.scale());
345: }
346:
347: /**
348: * new BigDecimal(-0.1)
349: */
350: public void testConstrDoubleMinus01() {
351: double a = -1.E-1;
352: int aScale = 55;
353: BigInteger bA = new BigInteger(
354: "-1000000000000000055511151231257827021181583404541015625");
355: BigDecimal aNumber = new BigDecimal(a);
356: assertEquals("incorrect value", bA, aNumber.unscaledValue());
357: assertEquals("incorrect scale", aScale, aNumber.scale());
358: }
359:
360: /**
361: * new BigDecimal(int value)
362: */
363: public void testConstrInt() {
364: int a = 732546982;
365: String res = "732546982";
366: int resScale = 0;
367: BigDecimal result = new BigDecimal(a);
368: assertEquals("incorrect value", res, result.unscaledValue()
369: .toString());
370: assertEquals("incorrect scale", resScale, result.scale());
371: }
372:
373: /**
374: * new BigDecimal(int, MathContext)
375: */
376: public void testConstrIntMathContext() {
377: int a = 732546982;
378: int precision = 21;
379: RoundingMode rm = RoundingMode.CEILING;
380: MathContext mc = new MathContext(precision, rm);
381: String res = "732546982";
382: int resScale = 0;
383: BigDecimal result = new BigDecimal(a, mc);
384: assertEquals("incorrect value", res, result.unscaledValue()
385: .toString());
386: assertEquals("incorrect scale", resScale, result.scale());
387: }
388:
389: /**
390: * new BigDecimal(long value)
391: */
392: public void testConstrLong() {
393: long a = 4576578677732546982L;
394: String res = "4576578677732546982";
395: int resScale = 0;
396: BigDecimal result = new BigDecimal(a);
397: assertEquals("incorrect value", res, result.unscaledValue()
398: .toString());
399: assertEquals("incorrect scale", resScale, result.scale());
400: }
401:
402: /**
403: * new BigDecimal(long, MathContext)
404: */
405: public void testConstrLongMathContext() {
406: long a = 4576578677732546982L;
407: int precision = 5;
408: RoundingMode rm = RoundingMode.CEILING;
409: MathContext mc = new MathContext(precision, rm);
410: String res = "45766";
411: int resScale = -14;
412: BigDecimal result = new BigDecimal(a, mc);
413: assertEquals("incorrect value", res, result.unscaledValue()
414: .toString());
415: assertEquals("incorrect scale", resScale, result.scale());
416: }
417:
418: /**
419: * new BigDecimal(double value) when value is denormalized
420: */
421: public void testConstrDoubleDenormalized() {
422: double a = 2.274341322658976E-309;
423: int aScale = 1073;
424: BigInteger bA = new BigInteger(
425: "227434132265897633950269241702666687639731047124115603942986140264569528085692462493371029187342478828091760934014851133733918639492582043963243759464684978401240614084312038547315281016804838374623558434472007664427140169018817050565150914041833284370702366055678057809362286455237716100382057360123091641959140448783514464639706721250400288267372238950016114583259228262046633530468551311769574111763316146065958042194569102063373243372766692713192728878701004405568459288708477607744497502929764155046100964958011009313090462293046650352146796805866786767887226278836423536035611825593567576424943331337401071583562754098901412372708947790843318760718495117047155597276492717187936854356663665005157041552436478744491526494952982062613955349661409854888916015625");
426: BigDecimal aNumber = new BigDecimal(a);
427: assertEquals("incorrect value", bA, aNumber.unscaledValue());
428: assertEquals("incorrect scale", aScale, aNumber.scale());
429: }
430:
431: /**
432: * new BigDecimal(String value)
433: * when value is not a valid representation of BigDecimal.
434: */
435: public void testConstrStringException() {
436: String a = "-238768.787678287a+10";
437: try {
438: new BigDecimal(a);
439: fail("NumberFormatException has not been caught");
440: } catch (NumberFormatException e) {
441: }
442: }
443:
444: /**
445: * new BigDecimal(String value) when exponent is empty.
446: */
447: public void testConstrStringExceptionEmptyExponent1() {
448: String a = "-238768.787678287e";
449: try {
450: new BigDecimal(a);
451: fail("NumberFormatException has not been caught");
452: } catch (NumberFormatException e) {
453: }
454: }
455:
456: /**
457: * new BigDecimal(String value) when exponent is empty.
458: */
459: public void testConstrStringExceptionEmptyExponent2() {
460: String a = "-238768.787678287e-";
461: try {
462: new BigDecimal(a);
463: fail("NumberFormatException has not been caught");
464: } catch (NumberFormatException e) {
465: }
466: }
467:
468: /**
469: * new BigDecimal(String value) when exponent is greater than
470: * Integer.MAX_VALUE.
471: */
472: public void testConstrStringExceptionExponentGreaterIntegerMax() {
473: String a = "-238768.787678287e214748364767876";
474: try {
475: new BigDecimal(a);
476: fail("NumberFormatException has not been caught");
477: } catch (NumberFormatException e) {
478: }
479: }
480:
481: /**
482: * new BigDecimal(String value) when exponent is less than
483: * Integer.MIN_VALUE.
484: */
485: public void testConstrStringExceptionExponentLessIntegerMin() {
486: String a = "-238768.787678287e-214748364767876";
487: try {
488: new BigDecimal(a);
489: fail("NumberFormatException has not been caught");
490: } catch (NumberFormatException e) {
491: }
492: }
493:
494: /**
495: * new BigDecimal(String value)
496: * when exponent is Integer.MAX_VALUE.
497: */
498: public void testConstrStringExponentIntegerMax() {
499: String a = "-238768.787678287e2147483647";
500: int aScale = -2147483638;
501: BigInteger bA = new BigInteger("-238768787678287");
502: BigDecimal aNumber = new BigDecimal(a);
503: assertEquals("incorrect value", bA, aNumber.unscaledValue());
504: assertEquals("incorrect scale", aScale, aNumber.scale());
505: }
506:
507: /**
508: * new BigDecimal(String value)
509: * when exponent is Integer.MIN_VALUE.
510: */
511: public void testConstrStringExponentIntegerMin() {
512: String a = ".238768e-2147483648";
513: try {
514: new BigDecimal(a);
515: fail("NumberFormatException expected");
516: } catch (NumberFormatException e) {
517: assertEquals("Improper exception message",
518: "Scale out of range.", e.getMessage());
519: }
520: }
521:
522: /**
523: * new BigDecimal(String value); value does not contain exponent
524: */
525: public void testConstrStringWithoutExpPos1() {
526: String a = "732546982374982347892379283571094797.287346782359284756";
527: int aScale = 18;
528: BigInteger bA = new BigInteger(
529: "732546982374982347892379283571094797287346782359284756");
530: BigDecimal aNumber = new BigDecimal(a);
531: assertEquals("incorrect value", bA, aNumber.unscaledValue());
532: assertEquals("incorrect scale", aScale, aNumber.scale());
533: }
534:
535: /**
536: * new BigDecimal(String value); value does not contain exponent
537: */
538: public void testConstrStringWithoutExpPos2() {
539: String a = "+732546982374982347892379283571094797.287346782359284756";
540: int aScale = 18;
541: BigInteger bA = new BigInteger(
542: "732546982374982347892379283571094797287346782359284756");
543: BigDecimal aNumber = new BigDecimal(a);
544: assertEquals("incorrect value", bA, aNumber.unscaledValue());
545: assertEquals("incorrect scale", aScale, aNumber.scale());
546: }
547:
548: /**
549: * new BigDecimal(String value); value does not contain exponent
550: */
551: public void testConstrStringWithoutExpNeg() {
552: String a = "-732546982374982347892379283571094797.287346782359284756";
553: int aScale = 18;
554: BigInteger bA = new BigInteger(
555: "-732546982374982347892379283571094797287346782359284756");
556: BigDecimal aNumber = new BigDecimal(a);
557: assertEquals("incorrect value", bA, aNumber.unscaledValue());
558: assertEquals("incorrect scale", aScale, aNumber.scale());
559: }
560:
561: /**
562: * new BigDecimal(String value); value does not contain exponent
563: * and decimal point
564: */
565: public void testConstrStringWithoutExpWithoutPoint() {
566: String a = "-732546982374982347892379283571094797287346782359284756";
567: int aScale = 0;
568: BigInteger bA = new BigInteger(
569: "-732546982374982347892379283571094797287346782359284756");
570: BigDecimal aNumber = new BigDecimal(a);
571: assertEquals("incorrect value", bA, aNumber.unscaledValue());
572: assertEquals("incorrect scale", aScale, aNumber.scale());
573: }
574:
575: /**
576: * new BigDecimal(String value); value contains exponent
577: * and does not contain decimal point
578: */
579: public void testConstrStringWithExponentWithoutPoint1() {
580: String a = "-238768787678287e214";
581: int aScale = -214;
582: BigInteger bA = new BigInteger("-238768787678287");
583: BigDecimal aNumber = new BigDecimal(a);
584: assertEquals("incorrect value", bA, aNumber.unscaledValue());
585: assertEquals("incorrect scale", aScale, aNumber.scale());
586: }
587:
588: /**
589: * new BigDecimal(String value); value contains exponent
590: * and does not contain decimal point
591: */
592: public void testConstrStringWithExponentWithoutPoint2() {
593: String a = "-238768787678287e-214";
594: int aScale = 214;
595: BigInteger bA = new BigInteger("-238768787678287");
596: BigDecimal aNumber = new BigDecimal(a);
597: assertEquals("incorrect value", bA, aNumber.unscaledValue());
598: assertEquals("incorrect scale", aScale, aNumber.scale());
599: }
600:
601: /**
602: * new BigDecimal(String value); value contains exponent
603: * and does not contain decimal point
604: */
605: public void testConstrStringWithExponentWithoutPoint3() {
606: String a = "238768787678287e-214";
607: int aScale = 214;
608: BigInteger bA = new BigInteger("238768787678287");
609: BigDecimal aNumber = new BigDecimal(a);
610: assertEquals("incorrect value", bA, aNumber.unscaledValue());
611: assertEquals("incorrect scale", aScale, aNumber.scale());
612: }
613:
614: /**
615: * new BigDecimal(String value); value contains exponent
616: * and does not contain decimal point
617: */
618: public void testConstrStringWithExponentWithoutPoint4() {
619: String a = "238768787678287e+214";
620: int aScale = -214;
621: BigInteger bA = new BigInteger("238768787678287");
622: BigDecimal aNumber = new BigDecimal(a);
623: assertEquals("incorrect value", bA, aNumber.unscaledValue());
624: assertEquals("incorrect scale", aScale, aNumber.scale());
625: }
626:
627: /**
628: * new BigDecimal(String value); value contains exponent
629: * and does not contain decimal point
630: */
631: public void testConstrStringWithExponentWithoutPoint5() {
632: String a = "238768787678287E214";
633: int aScale = -214;
634: BigInteger bA = new BigInteger("238768787678287");
635: BigDecimal aNumber = new BigDecimal(a);
636: assertEquals("incorrect value", bA, aNumber.unscaledValue());
637: assertEquals("incorrect scale", aScale, aNumber.scale());
638: }
639:
640: /**
641: * new BigDecimal(String value);
642: * value contains both exponent and decimal point
643: */
644: public void testConstrStringWithExponentWithPoint1() {
645: String a = "23985439837984782435652424523876878.7678287e+214";
646: int aScale = -207;
647: BigInteger bA = new BigInteger(
648: "239854398379847824356524245238768787678287");
649: BigDecimal aNumber = new BigDecimal(a);
650: assertEquals("incorrect value", bA, aNumber.unscaledValue());
651: assertEquals("incorrect scale", aScale, aNumber.scale());
652: }
653:
654: /**
655: * new BigDecimal(String value);
656: * value contains both exponent and decimal point
657: */
658: public void testConstrStringWithExponentWithPoint2() {
659: String a = "238096483923847545735673567457356356789029578490276878.7678287e-214";
660: int aScale = 221;
661: BigInteger bA = new BigInteger(
662: "2380964839238475457356735674573563567890295784902768787678287");
663: BigDecimal aNumber = new BigDecimal(a);
664: assertEquals("incorrect value", bA, aNumber.unscaledValue());
665: assertEquals("incorrect scale", aScale, aNumber.scale());
666: }
667:
668: /**
669: * new BigDecimal(String value);
670: * value contains both exponent and decimal point
671: */
672: public void testConstrStringWithExponentWithPoint3() {
673: String a = "2380964839238475457356735674573563567890.295784902768787678287E+21";
674: int aScale = 0;
675: BigInteger bA = new BigInteger(
676: "2380964839238475457356735674573563567890295784902768787678287");
677: BigDecimal aNumber = new BigDecimal(a);
678: assertEquals("incorrect value", bA, aNumber.unscaledValue());
679: assertEquals("incorrect scale", aScale, aNumber.scale());
680: }
681:
682: /**
683: * new BigDecimal(String value);
684: * value contains both exponent and decimal point
685: */
686: public void testConstrStringWithExponentWithPoint4() {
687: String a = "23809648392384754573567356745735635678.90295784902768787678287E+21";
688: int aScale = 2;
689: BigInteger bA = new BigInteger(
690: "2380964839238475457356735674573563567890295784902768787678287");
691: BigDecimal aNumber = new BigDecimal(a);
692: assertEquals("incorrect value", bA, aNumber.unscaledValue());
693: assertEquals("incorrect scale", aScale, aNumber.scale());
694: }
695:
696: /**
697: * new BigDecimal(String value);
698: * value contains both exponent and decimal point
699: */
700: public void testConstrStringWithExponentWithPoint5() {
701: String a = "238096483923847545735673567457356356789029.5784902768787678287E+21";
702: int aScale = -2;
703: BigInteger bA = new BigInteger(
704: "2380964839238475457356735674573563567890295784902768787678287");
705: BigDecimal aNumber = new BigDecimal(a);
706: assertEquals("incorrect value", bA, aNumber.unscaledValue());
707: assertEquals("incorrect scale", aScale, aNumber.scale());
708: }
709:
710: /**
711: * new BigDecimal(String value, MathContext)
712: */
713: public void testConstrStringMathContext() {
714: String a = "-238768787678287e214";
715: int precision = 5;
716: RoundingMode rm = RoundingMode.CEILING;
717: MathContext mc = new MathContext(precision, rm);
718: String res = "-23876";
719: int resScale = -224;
720: BigDecimal result = new BigDecimal(a, mc);
721: assertEquals("incorrect value", res, result.unscaledValue()
722: .toString());
723: assertEquals("incorrect scale", resScale, result.scale());
724: }
725: }
|