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);
}
/**

View File

@ -109,7 +109,6 @@ import javafx.collections.ListChangeListener;
import javafx.util.Callback;
import javafx.util.StringConverter;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Comparator;
import java.util.Map;
@ -306,7 +305,7 @@ abstract public class OfferBookView<R extends GridPane, M extends OfferBookViewM
if ((deposit == null || amountValue == 0)) {
return 0d;
} else {
return BigDecimal.valueOf(deposit.longValueExact()).divide(BigDecimal.valueOf(amountValue)).doubleValue();
return HavenoUtils.divide(deposit, BigInteger.valueOf(amountValue));
}
}, Comparator.nullsFirst(Comparator.naturalOrder())));

View File

@ -72,7 +72,6 @@ import javafx.collections.ObservableList;
import javafx.collections.transformation.FilteredList;
import javafx.collections.transformation.SortedList;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.DecimalFormat;
@ -633,7 +632,7 @@ abstract class OfferBookViewModel extends ActivatableViewModel {
}
public String formatDepositString(BigInteger deposit, long amount) {
var percentage = FormattingUtils.formatToRoundedPercentWithSymbol(BigDecimal.valueOf(deposit.longValueExact()).divide(BigDecimal.valueOf(amount)).doubleValue());
var percentage = FormattingUtils.formatToRoundedPercentWithSymbol(HavenoUtils.divide(deposit, BigInteger.valueOf(amount)));
return HavenoUtils.formatXmr(deposit) + " (" + percentage + ")";
}

View File

@ -57,6 +57,7 @@ import haveno.core.locale.TradeCurrency;
import haveno.core.payment.PaymentAccount;
import haveno.core.payment.PaymentAccountList;
import haveno.core.payment.payload.PaymentMethod;
import haveno.core.trade.HavenoUtils;
import haveno.core.trade.txproof.AssetTxProofResult;
import haveno.core.user.DontShowAgainLookup;
import haveno.core.user.Preferences;
@ -655,7 +656,7 @@ public class GUIUtil {
}
public static String getPercentage(BigInteger part, BigInteger total) {
return FormattingUtils.formatToPercentWithSymbol(new BigDecimal(part).divide(new BigDecimal(total)).doubleValue());
return FormattingUtils.formatToPercentWithSymbol(HavenoUtils.divide(part, total));
}
public static <T> T getParentOfType(Node node, Class<T> t) {