mirror of
https://github.com/haveno-dex/haveno.git
synced 2024-11-17 08:17:57 +00:00
prevent non-terminating BigDecimal division
This commit is contained in:
parent
47f3d98597
commit
60341002fd
6 changed files with 12 additions and 13 deletions
|
@ -78,7 +78,7 @@ public class ClosedTradableFormatter {
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getTotalTxFeeAsString(BigInteger totalTradeAmount, BigInteger totalTxFee) {
|
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,
|
return Res.get(I18N_KEY_TOTAL_TX_FEE,
|
||||||
HavenoUtils.formatXmr(totalTxFee, true),
|
HavenoUtils.formatXmr(totalTxFee, true),
|
||||||
formatToPercentWithSymbol(percentage));
|
formatToPercentWithSymbol(percentage));
|
||||||
|
@ -98,7 +98,7 @@ public class ClosedTradableFormatter {
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getTotalTradeFeeAsString(BigInteger totalTradeAmount, BigInteger totalTradeFee) {
|
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,
|
return Res.get(I18N_KEY_TOTAL_TRADE_FEE_BTC,
|
||||||
HavenoUtils.formatXmr(totalTradeFee, true),
|
HavenoUtils.formatXmr(totalTradeFee, true),
|
||||||
formatToPercentWithSymbol(percentage));
|
formatToPercentWithSymbol(percentage));
|
||||||
|
|
|
@ -129,6 +129,9 @@ public class HavenoUtils {
|
||||||
return atomicUnitsToXmr(coinToAtomicUnits(coin));
|
return atomicUnitsToXmr(coinToAtomicUnits(coin));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static double divide(BigInteger auDividend, BigInteger auDivisor) {
|
||||||
|
return (double) atomicUnitsToCentineros(auDividend) / (double) atomicUnitsToCentineros(auDivisor);
|
||||||
|
}
|
||||||
|
|
||||||
// ------------------------- FORMAT UTILS ---------------------------------
|
// ------------------------- FORMAT UTILS ---------------------------------
|
||||||
|
|
||||||
|
@ -230,9 +233,8 @@ public class HavenoUtils {
|
||||||
|
|
||||||
public static BigInteger getFeePerXmr(BigInteger feePerXmr, BigInteger amount) {
|
public static BigInteger getFeePerXmr(BigInteger feePerXmr, BigInteger amount) {
|
||||||
BigDecimal feePerXmrAsDecimal = feePerXmr == null ? BigDecimal.valueOf(0) : new BigDecimal(feePerXmr);
|
BigDecimal feePerXmrAsDecimal = feePerXmr == null ? BigDecimal.valueOf(0) : new BigDecimal(feePerXmr);
|
||||||
BigDecimal amountAsDecimal = amount == null ? BigDecimal.valueOf(0) : new BigDecimal(amount);
|
BigDecimal amountMultiplier = BigDecimal.valueOf(divide(amount == null ? BigInteger.valueOf(0) : amount, HavenoUtils.xmrToAtomicUnits(1.0)));
|
||||||
BigDecimal xmrAsDecimal = new BigDecimal(HavenoUtils.xmrToAtomicUnits(1.0));
|
return feePerXmrAsDecimal.multiply(amountMultiplier).toBigInteger();
|
||||||
return feePerXmrAsDecimal.multiply(amountAsDecimal.divide(xmrAsDecimal)).toBigInteger();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -64,9 +64,7 @@ public class CoinUtil {
|
||||||
* @return The percentage value as double (e.g. 1% is 0.01)
|
* @return The percentage value as double (e.g. 1% is 0.01)
|
||||||
*/
|
*/
|
||||||
public static double getAsPercentPerBtc(BigInteger part, BigInteger total) {
|
public static double getAsPercentPerBtc(BigInteger part, BigInteger total) {
|
||||||
BigDecimal partDecimal = part == null ? BigDecimal.valueOf(0) : new BigDecimal(part);
|
return MathUtils.roundDouble(HavenoUtils.divide(part == null ? BigInteger.valueOf(0) : part, total == null ? BigInteger.valueOf(1) : total), 4);
|
||||||
BigDecimal totalDecimal = total == null ? BigDecimal.valueOf(1) : new BigDecimal(total);
|
|
||||||
return MathUtils.roundDouble(partDecimal.divide(totalDecimal).doubleValue(), 4);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -109,7 +109,6 @@ import javafx.collections.ListChangeListener;
|
||||||
import javafx.util.Callback;
|
import javafx.util.Callback;
|
||||||
import javafx.util.StringConverter;
|
import javafx.util.StringConverter;
|
||||||
|
|
||||||
import java.math.BigDecimal;
|
|
||||||
import java.math.BigInteger;
|
import java.math.BigInteger;
|
||||||
import java.util.Comparator;
|
import java.util.Comparator;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
@ -306,7 +305,7 @@ abstract public class OfferBookView<R extends GridPane, M extends OfferBookViewM
|
||||||
if ((deposit == null || amountValue == 0)) {
|
if ((deposit == null || amountValue == 0)) {
|
||||||
return 0d;
|
return 0d;
|
||||||
} else {
|
} else {
|
||||||
return BigDecimal.valueOf(deposit.longValueExact()).divide(BigDecimal.valueOf(amountValue)).doubleValue();
|
return HavenoUtils.divide(deposit, BigInteger.valueOf(amountValue));
|
||||||
}
|
}
|
||||||
|
|
||||||
}, Comparator.nullsFirst(Comparator.naturalOrder())));
|
}, Comparator.nullsFirst(Comparator.naturalOrder())));
|
||||||
|
|
|
@ -72,7 +72,6 @@ import javafx.collections.ObservableList;
|
||||||
import javafx.collections.transformation.FilteredList;
|
import javafx.collections.transformation.FilteredList;
|
||||||
import javafx.collections.transformation.SortedList;
|
import javafx.collections.transformation.SortedList;
|
||||||
|
|
||||||
import java.math.BigDecimal;
|
|
||||||
import java.math.BigInteger;
|
import java.math.BigInteger;
|
||||||
import java.text.DecimalFormat;
|
import java.text.DecimalFormat;
|
||||||
|
|
||||||
|
@ -633,7 +632,7 @@ abstract class OfferBookViewModel extends ActivatableViewModel {
|
||||||
}
|
}
|
||||||
|
|
||||||
public String formatDepositString(BigInteger deposit, long amount) {
|
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 + ")";
|
return HavenoUtils.formatXmr(deposit) + " (" + percentage + ")";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -57,6 +57,7 @@ import haveno.core.locale.TradeCurrency;
|
||||||
import haveno.core.payment.PaymentAccount;
|
import haveno.core.payment.PaymentAccount;
|
||||||
import haveno.core.payment.PaymentAccountList;
|
import haveno.core.payment.PaymentAccountList;
|
||||||
import haveno.core.payment.payload.PaymentMethod;
|
import haveno.core.payment.payload.PaymentMethod;
|
||||||
|
import haveno.core.trade.HavenoUtils;
|
||||||
import haveno.core.trade.txproof.AssetTxProofResult;
|
import haveno.core.trade.txproof.AssetTxProofResult;
|
||||||
import haveno.core.user.DontShowAgainLookup;
|
import haveno.core.user.DontShowAgainLookup;
|
||||||
import haveno.core.user.Preferences;
|
import haveno.core.user.Preferences;
|
||||||
|
@ -655,7 +656,7 @@ public class GUIUtil {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String getPercentage(BigInteger part, BigInteger total) {
|
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) {
|
public static <T> T getParentOfType(Node node, Class<T> t) {
|
||||||
|
|
Loading…
Reference in a new issue