how to show all decimals of a division like 1/41 or 2/149 since I use BigDecimal and throw me error

1
  

Exception in thread "main" java.lang.ArithmeticException:   Non-terminating decimal expansion; not exact decimal representable   result.

It shows me this error when doing a division 1/41 and it results in 50 decimals since I want to show them all but it does not show them that I can show them another way it is assumed that the BigDecimal is to show the figures Exactly if someone can help me thanks

    
asked by Yeison Osorio 15.04.2017 в 00:19
source

1 answer

1

Using the class BigDecimal () it is possible to perform this operation , applying the method divide which will be sent three parameters: numero2 , cantidad de cifras decimales and Type of rounding

The class BigDecimal by default always tries to return the result Exactly, when performing the division with infinite decimals it will throw said Exception. To solve this, it is necessary to specify the precisión(cantidad de cifras decimales) . and also the Type of rounding for the division to the division. HALF_UP in this case.

BigDecimal num1 = new BigDecimal(1);
BigDecimal num2 = new BigDecimal(41);
BigDecimal result = num1.divide(num2,50,RoundingMode.HALF_UP);
System.out.println(result.toString());

For more details, here

    
answered by 15.04.2017 в 00:36