prevent non-terminating BigDecimal division

This commit is contained in:
woodser 2023-03-09 12:08:30 -05:00
parent 47f3d98597
commit 60341002fd
6 changed files with 12 additions and 13 deletions

View file

@ -78,7 +78,7 @@ public class ClosedTradableFormatter {
}
public String getTotalTxFeeAsString(BigInteger totalTradeAmount, BigInteger totalTxFee) {
double percentage = new BigDecimal(totalTxFee).divide(new BigDecimal(totalTradeAmount)).doubleValue();
double percentage = HavenoUtils.divide(totalTxFee, totalTradeAmount);
return Res.get(I18N_KEY_TOTAL_TX_FEE,
HavenoUtils.formatXmr(totalTxFee, true),
formatToPercentWithSymbol(percentage));
@ -98,7 +98,7 @@ public class ClosedTradableFormatter {
}
public String getTotalTradeFeeAsString(BigInteger totalTradeAmount, BigInteger totalTradeFee) {
double percentage = new BigDecimal(totalTradeFee).divide(new BigDecimal(totalTradeAmount)).doubleValue();
double percentage = HavenoUtils.divide(totalTradeFee, totalTradeAmount);
return Res.get(I18N_KEY_TOTAL_TRADE_FEE_BTC,
HavenoUtils.formatXmr(totalTradeFee, true),
formatToPercentWithSymbol(percentage));

View file

@ -129,6 +129,9 @@ public class HavenoUtils {
return atomicUnitsToXmr(coinToAtomicUnits(coin));
}
public static double divide(BigInteger auDividend, BigInteger auDivisor) {
return (double) atomicUnitsToCentineros(auDividend) / (double) atomicUnitsToCentineros(auDivisor);
}
// ------------------------- FORMAT UTILS ---------------------------------
@ -230,9 +233,8 @@ public class HavenoUtils {
public static BigInteger getFeePerXmr(BigInteger feePerXmr, BigInteger amount) {
BigDecimal feePerXmrAsDecimal = feePerXmr == null ? BigDecimal.valueOf(0) : new BigDecimal(feePerXmr);
BigDecimal amountAsDecimal = amount == null ? BigDecimal.valueOf(0) : new BigDecimal(amount);
BigDecimal xmrAsDecimal = new BigDecimal(HavenoUtils.xmrToAtomicUnits(1.0));
return feePerXmrAsDecimal.multiply(amountAsDecimal.divide(xmrAsDecimal)).toBigInteger();
BigDecimal amountMultiplier = BigDecimal.valueOf(divide(amount == null ? BigInteger.valueOf(0) : amount, HavenoUtils.xmrToAtomicUnits(1.0)));
return feePerXmrAsDecimal.multiply(amountMultiplier).toBigInteger();
}

View file

@ -64,9 +64,7 @@ public class CoinUtil {
* @return The percentage value as double (e.g. 1% is 0.01)
*/
public static double getAsPercentPerBtc(BigInteger part, BigInteger total) {
BigDecimal partDecimal = part == null ? BigDecimal.valueOf(0) : new BigDecimal(part);
BigDecimal totalDecimal = total == null ? BigDecimal.valueOf(1) : new BigDecimal(total);
return MathUtils.roundDouble(partDecimal.divide(totalDecimal).doubleValue(), 4);
return MathUtils.roundDouble(HavenoUtils.divide(part == null ? BigInteger.valueOf(0) : part, total == null ? BigInteger.valueOf(1) : total), 4);
}
/**