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

@ -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) {