-1

I'm using Math.Context in my coding. Now the problem is Math.Context is calculating everything after decimal. I want to define Scale of 8 (means 8 digits after decimal point) for Math.Context. Please note i'm not looking for precision. I know how to define precision. I need scale.

This is my Math.Context code line :

answer =  firstvalue.multiply(secondvalue).divide(new BigDecimal("1240"), MathContext.DECIMAL32);
  • Please [edit] your question to include the full source code you have as a [mcve]. Show the example inputs you have and the result you want to get. Also write the possible input values you will have like what are the minimum and maximum values. Having always 8 decimal point digits would result in a dynamic precision depending on the values. You also can consider using integer calculation and divide the end result in the end by `10^8` to get 8 decimal point digits. – Progman Jul 26 '20 at 09:46
  • You don't. `MathContext` doesn't have a 'scale' property. But `BigDecimal` does. – user207421 Jul 26 '20 at 10:05

2 Answers2

0

There is no argument in the constructor or helper method in MathContext class to set scale. you can try BigDecimal where scale can be given as

In divide argument the parameters can be passed as follows,

BigDecimal divide(BigDecimal divisor, int scale, RoundingMode roundingMode) i.e

firstvalue.multiply(secondvalue).divide(new BigDecimal("1240"), 4, RoundingMode.FLOOR);

Reference to MathContext javadoc: https://docs.oracle.com/javase/7/docs/api/java/math/MathContext.html

Hrudayanath
  • 474
  • 4
  • 18
0

You can do what you're asking. I show you how but before that i would like to tell you that you have to setScale() on BigDecimal. In this case I'm assuming that answer variable is BigDecimal . Here is your answer

answer =  firstvalue.multiply(secondvalue).divide(new BigDecimal("1240"), MathContext.DECIMAL32);
answer = answer.setScale(5, RoundingMode.HALF_EVEN); 
// YOU CAN SET YOUR OWN DESIRED SCALE OR ROUNDINGMODE
CODAR747
  • 230
  • 1
  • 12