My Java program is centered around high precision calculations, which need to be accurate to at least 120 decimal places.
Consequentially, all non-integer numbers will be represented by BigDecimals in the program.
Obviously I need to specify the accuracy of the rounding for the BigDecimals, to avoid infinite decimal expressions etc.
Currently, I find it a massive nuisance to have to specify the accuracy at every instantiation or mathematical operation of a BigDecimal.
Is there a way to set a ‘global accuracy’ for all BigDecimal calculations?
(Such as the Context.prec() for the Decimal module in python)
Thanks
Specs:
Java jre7 SE
Windows 7 (32)
(Almost) Original
Not as simple, but you can create a
MathContextand pass it to all yourBigDecimalconstructors and the methods performing operations.Revised
Alternatively, you can extend
BigDecimaland override any operations you want to use by supplying the rightMathContext, and using the rounding version ofdivide: