From 35b717756d1ba59a111ff6cd8e757cba9359a81c Mon Sep 17 00:00:00 2001 From: m2049r Date: Thu, 21 Nov 2019 10:42:16 +0100 Subject: [PATCH] Fix amount bugs (#645) * fix rounding error on send * fix check funds bug --- .../main/java/com/m2049r/xmrwallet/util/Helper.java | 1 + .../com/m2049r/xmrwallet/widget/ExchangeEditText.java | 11 ++++++----- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/com/m2049r/xmrwallet/util/Helper.java b/app/src/main/java/com/m2049r/xmrwallet/util/Helper.java index fe70b416..8b74ce2a 100644 --- a/app/src/main/java/com/m2049r/xmrwallet/util/Helper.java +++ b/app/src/main/java/com/m2049r/xmrwallet/util/Helper.java @@ -227,6 +227,7 @@ public class Helper { return String.format(Locale.US, "%,.2f", amount); else { // amount < 1 int decimals = 1 - (int) Math.floor(Math.log10(amount)); + if (decimals < 2) decimals = 2; if (decimals > 12) decimals = 12; return String.format(Locale.US, "%,." + decimals + "f", amount); } diff --git a/app/src/main/java/com/m2049r/xmrwallet/widget/ExchangeEditText.java b/app/src/main/java/com/m2049r/xmrwallet/widget/ExchangeEditText.java index accb3c6a..364aca7b 100644 --- a/app/src/main/java/com/m2049r/xmrwallet/widget/ExchangeEditText.java +++ b/app/src/main/java/com/m2049r/xmrwallet/widget/ExchangeEditText.java @@ -44,6 +44,7 @@ import com.m2049r.xmrwallet.util.Helper; import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import java.util.Locale; import timber.log.Timber; @@ -66,9 +67,9 @@ public class ExchangeEditText extends LinearLayout { return false; } boolean ok = true; - String enteredAmount = etAmountA.getText().toString(); + String nativeAmount = getNativeAmount(); try { - double amount = Double.parseDouble(enteredAmount); + double amount = Double.parseDouble(nativeAmount); if ((amount < min) || (amount > max)) { ok = false; } @@ -110,7 +111,7 @@ public class ExchangeEditText extends LinearLayout { public String getNativeAmount() { if (isExchangeInProgress()) return null; - if (sCurrencyA.getSelectedItemPosition() == 0) + if (getCurrencyA() == 0) return getCleanAmountString(etAmountA.getText().toString()); else return getCleanAmountString(tvAmountB.getText().toString()); @@ -336,7 +337,7 @@ public class ExchangeEditText extends LinearLayout { private void exchange(double rate) { double amount = getEnteredAmount(); if (rate > 0) { - tvAmountB.setText(Helper.getFormattedAmount(rate * amount)); + tvAmountB.setText(Helper.getFormattedAmount(rate * amount, getCurrencyB() == 0)); } else { tvAmountB.setText(null); Timber.w("No rate!"); @@ -347,7 +348,7 @@ public class ExchangeEditText extends LinearLayout { try { double amount = Double.parseDouble(enteredAmount); if (amount >= 0) { - return Helper.getFormattedAmount(amount); + return String.format(Locale.US, "%,.12f", amount); } else { return null; }