Why is there a BigDecimal.setScale()
method which sets the decimals to some value and no MathContext.setScale()
method but only a constructor MathContext(precision)
or other way around why is there no BigDecimal.setPrecision()
method?
EDIT:
There is no method MathContext.setPrecision()
, only a constructor MathContext(precision)
. Edited the text. Sorry for that.
EDIT2: I just wanted to understand why there are two ways of approaching a scale / precision setting. Seems there is no real reason to have two of them regarding the answer of Richard Sitze.
See this example which makes clear you can really have strange results playign with precision and scale:
MathContext mc = new MathContext(2);
BigDecimal test = new BigDecimal("100.234", mc);
System.out.println("Scale: " + test.scale() + ", value: " + test);
test.setScale(6);
System.out.println("Scale: " + test.scale() + ", value: " + test);
test = test.add(new BigDecimal("100"));
System.out.println("Scale: " + test.scale() + ", value: " + test);
Results in:
Scale: -1, value: 1.0E+2
Scale: -1, value: 1.0E+2
Scale: 0, value: 200
The results are correct regarding the settings but when I set the scale to 6 I expect a warning / hint / exception which says that the precision settings do not allow a scale of 6.