I have to show BigDecimal
value as integer if it doesnt'n have decimal part.
My solution generally works but the problem is when bigDecResult
receives
value with severals zeros after the point, like 0.0000
.
BigDecimal bigDecResult = new BigDecimal("0.0000");
if (bigDecResult.remainder(BigDecimal.ONE).equals(0)) {
System.out.println(Integer.toString(bigDecResult.intValue()));
} else {
System.out.println(bigDecResult.toString());
}
How can I receive 0
in case of bigDecResult = new BigDecimal("0.0000")
?
BigDecimal bd = new BigDecimal("0.0000");
if (bd.doubleValue() % 1 == 0.0 ){
System.out.println(bd.setScale(0));
// You can also convert to Integer:
// Integer response = bd.setScale(0).toBigInteger().intValue();
} else {
System.out.println(bd.setScale(4));
}