java - Why is there no BigDecimal.setPrecision() method? -
why there bigdecimal.setscale()
method sets decimals value , no mathcontext.setscale()
method constructor mathcontext(precision)
or other way around why there no bigdecimal.setprecision()
method?
edit: there no method mathcontext.setprecision()
, constructor mathcontext(precision)
. edited text. sorry that.
edit2: wanted understand why there 2 ways of approaching scale / precision setting. seems there no real reason have 2 of them regarding answer of richard sitze.
see example makes clear can have strange results playign precision , 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 correct regarding settings when set scale 6 expect warning / hint / exception says precision settings not allow scale of 6.
the powers-that-be declared overall precision (total # of digits) can adjusted incidentally changing scale (# of digits right of decimal point).
this appropriate, change precision necessitates change scale. @ best setprecision
redundant; having setscale
has resulted in cleaner api.
now consider change precision , how result might need rounded. in going through analysis, consider "how many digits should remain after rounding". is scale. such consideration leads question of how scale should changed.
setscale
variants specify rounding natural fit. less hypothetical setprecision
, begs code comments fill gap (imho).
Comments
Post a Comment