Here is a test case. Real-world situation is a calculation of a percentage of the total sum.
// allocation is 30%
double allocation = 30;
// total value
Money totalValue = MoneyFactory.fromString("7281.6612");
// money value of 30% allocation
double value = allocation * totalValue.toDouble() / 100;
Money moneyValue = MoneyFactory.fromDouble(value);
// calculate the percentage of the money value
double currentAllocationD = moneyValue.multiply(100)
.divide(totalValue.toDouble(), Constants.DEFAULT_PRECISION)
.toDouble();
Money currentAllocation = moneyValue.multiply(100)
.divide(totalValue.toDouble(), Constants.DEFAULT_PRECISION);
// it should be 30, as set initially.
assertThat(currentAllocationD).isEqualTo(30);
assertThat(currentAllocation.toString()).isEqualTo("30");
The wrong number comes out of the multiplication with 100.
Here is a test case. Real-world situation is a calculation of a percentage of the total sum.
The wrong number comes out of the multiplication with 100.