if we divide a BigDecimal number with scientific notation by another number, the result is not accurate:
BigDecimal bigDecimal = new BigDecimal("1.3E+4");
BigDecimal divide = bigDecimal.divide(BigDecimal.TEN, RoundingMode.HALF_EVEN);
System.out.println("The result is wrong: " + divide); //The result is wrong: 1E+3
Edit
I expected the divide
value to be equal to 1300
but it's equal to 1000
.
If I use bigDecimal.setScale(0).divide(...)
the problem will be solved but another problem will be raised:
BigDecimal bigDecimal = new BigDecimal("1.3E+4");
BigDecimal divide = bigDecimal.setScale(0,RoundingMode.HALF_EVEN).divide(BigDecimal.TEN, RoundingMode.HALF_EVEN);
System.out.println("The result is OK: " + divide); //The result is OK: 1300
BigDecimal bigDecimal2 = new BigDecimal("13.0");
BigDecimal correctResult = bigDecimal2.divide(BigDecimal.valueOf(3), RoundingMode.HALF_EVEN);
BigDecimal wrongResult = bigDecimal2.setScale(0,RoundingMode.HALF_EVEN).divide(BigDecimal.valueOf(3), RoundingMode.HALF_EVEN);
System.out.println("Correct result: "+correctResult); //Correct result: 4.3
System.out.println("Wrong result: "+wrongResult); //Wrong result: 4
How I can do a reliable divide?