Fix amount bugs (#645)

* fix rounding error on send

* fix check funds bug
This commit is contained in:
m2049r 2019-11-21 10:42:16 +01:00 committed by GitHub
parent c14486306e
commit 35b717756d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 7 additions and 5 deletions

View File

@ -227,6 +227,7 @@ public class Helper {
return String.format(Locale.US, "%,.2f", amount); return String.format(Locale.US, "%,.2f", amount);
else { // amount < 1 else { // amount < 1
int decimals = 1 - (int) Math.floor(Math.log10(amount)); int decimals = 1 - (int) Math.floor(Math.log10(amount));
if (decimals < 2) decimals = 2;
if (decimals > 12) decimals = 12; if (decimals > 12) decimals = 12;
return String.format(Locale.US, "%,." + decimals + "f", amount); return String.format(Locale.US, "%,." + decimals + "f", amount);
} }

View File

@ -44,6 +44,7 @@ import com.m2049r.xmrwallet.util.Helper;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Locale;
import timber.log.Timber; import timber.log.Timber;
@ -66,9 +67,9 @@ public class ExchangeEditText extends LinearLayout {
return false; return false;
} }
boolean ok = true; boolean ok = true;
String enteredAmount = etAmountA.getText().toString(); String nativeAmount = getNativeAmount();
try { try {
double amount = Double.parseDouble(enteredAmount); double amount = Double.parseDouble(nativeAmount);
if ((amount < min) || (amount > max)) { if ((amount < min) || (amount > max)) {
ok = false; ok = false;
} }
@ -110,7 +111,7 @@ public class ExchangeEditText extends LinearLayout {
public String getNativeAmount() { public String getNativeAmount() {
if (isExchangeInProgress()) return null; if (isExchangeInProgress()) return null;
if (sCurrencyA.getSelectedItemPosition() == 0) if (getCurrencyA() == 0)
return getCleanAmountString(etAmountA.getText().toString()); return getCleanAmountString(etAmountA.getText().toString());
else else
return getCleanAmountString(tvAmountB.getText().toString()); return getCleanAmountString(tvAmountB.getText().toString());
@ -336,7 +337,7 @@ public class ExchangeEditText extends LinearLayout {
private void exchange(double rate) { private void exchange(double rate) {
double amount = getEnteredAmount(); double amount = getEnteredAmount();
if (rate > 0) { if (rate > 0) {
tvAmountB.setText(Helper.getFormattedAmount(rate * amount)); tvAmountB.setText(Helper.getFormattedAmount(rate * amount, getCurrencyB() == 0));
} else { } else {
tvAmountB.setText(null); tvAmountB.setText(null);
Timber.w("No rate!"); Timber.w("No rate!");
@ -347,7 +348,7 @@ public class ExchangeEditText extends LinearLayout {
try { try {
double amount = Double.parseDouble(enteredAmount); double amount = Double.parseDouble(enteredAmount);
if (amount >= 0) { if (amount >= 0) {
return Helper.getFormattedAmount(amount); return String.format(Locale.US, "%,.12f", amount);
} else { } else {
return null; return null;
} }