Skip to content

Commit

Permalink
Merge pull request #478 from rivaldi8/bugfix-474-exchange-rate-decima…
Browse files Browse the repository at this point in the history
…l-places

Bugfix #474 - Exchange rate limited to two decimal places when tranferring between accounts with different currencies
  • Loading branch information
codinguser committed Feb 23, 2016
2 parents 1ffae9f + 33b31da commit e7ee18e
Show file tree
Hide file tree
Showing 35 changed files with 100 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@

import java.math.BigDecimal;
import java.math.MathContext;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.Currency;

import butterknife.Bind;
Expand Down Expand Up @@ -103,8 +106,10 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
mToCurrencyLabel.setText(mTargetCurrency.getCurrencyCode());
mConvertedAmountCurrencyLabel.setText(mTargetCurrency.getCurrencyCode());

mSampleExchangeRate.setText("e.g. 1 " + fromCurrency.getCurrencyCode() + " = " + " x.xx " + mTargetCurrency.getCurrencyCode());
final InputWatcher textChangeListener = new InputWatcher();
mSampleExchangeRate.setText(String.format(getString(R.string.sample_exchange_rate),
fromCurrency.getCurrencyCode(),
mTargetCurrency.getCurrencyCode()));
final InputLayoutErrorClearer textChangeListener = new InputLayoutErrorClearer();

CommoditiesDbAdapter commoditiesDbAdapter = CommoditiesDbAdapter.getInstance();
String commodityUID = commoditiesDbAdapter.getCommodityUID(fromCurrency.getCurrencyCode());
Expand All @@ -115,14 +120,17 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa

if (price.first > 0 && price.second > 0) {
// a valid price exists
BigDecimal num = new BigDecimal(price.first);
BigDecimal denom = new BigDecimal(price.second);
mExchangeRateInput.setText(num.divide(denom, MathContext.DECIMAL32).toString());
mConvertedAmountInput.setText(mOriginAmount.asBigDecimal().multiply(num).divide(denom, currencyCommodity.getSmallestFractionDigits(), BigDecimal.ROUND_HALF_EVEN).toString());
BigDecimal numerator = new BigDecimal(price.first);
BigDecimal denominator = new BigDecimal(price.second);
DecimalFormat formatter = (DecimalFormat) NumberFormat.getNumberInstance();
mExchangeRateInput.setText(formatter.format(numerator.divide(denominator, MathContext.DECIMAL32)));
// convertedAmount = mOriginAmount * numerator / denominator
BigDecimal convertedAmount = mOriginAmount.asBigDecimal().multiply(numerator)
.divide(denominator, currencyCommodity.getSmallestFractionDigits(), BigDecimal.ROUND_HALF_EVEN);
mConvertedAmountInput.setText(formatter.format(convertedAmount));
}

mExchangeRateInput.addTextChangedListener(textChangeListener);
mExchangeRateInput.addTextChangedListener(new AmountInputFormatter(mExchangeRateInput));
mConvertedAmountInput.addTextChangedListener(textChangeListener);
mConvertedAmountInput.addTextChangedListener(new AmountInputFormatter(mConvertedAmountInput));

Expand Down Expand Up @@ -185,15 +193,18 @@ public Dialog onCreateDialog(Bundle savedInstanceState) {
/**
* Converts the currency amount with the given exchange rate and saves the price to the db
*/
private void transferFunds(){
private void transferFunds() {
if (mExchangeRateRadioButton.isChecked()){
String exchangeRateString = mExchangeRateInput.getText().toString();
if (exchangeRateString.isEmpty()){
mExchangeRateInputLayout.setError(getString(R.string.error_exchange_rate_required));
DecimalFormat formatter = (DecimalFormat) NumberFormat.getNumberInstance();
formatter.setParseBigDecimal(true);
BigDecimal rate;
try {
rate = (BigDecimal) formatter.parse(exchangeRateString);
} catch (ParseException e) {
mExchangeRateInputLayout.setError(getString(R.string.error_invalid_exchange_rate));
return;
}

BigDecimal rate = TransactionFormFragment.parseInputToDecimal(exchangeRateString);
mConvertedAmount = mOriginAmount.multiply(rate);
}

Expand Down Expand Up @@ -225,17 +236,16 @@ private void transferFunds(){
dismiss();
}

private class InputWatcher implements TextWatcher {

/**
* Hides the error message from mConvertedAmountInputLayout and mExchangeRateInputLayout
* when the user edits their content.
*/
private class InputLayoutErrorClearer implements TextWatcher {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {

}
public void beforeTextChanged(CharSequence s, int start, int count, int after) { }

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {

}
public void onTextChanged(CharSequence s, int start, int before, int count) { }

@Override
public void afterTextChanged(Editable s) {
Expand Down
5 changes: 4 additions & 1 deletion app/src/main/res/layout/dialog_transfer_funds.xml
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,14 @@
android:layout_width="0dp"
android:layout_weight="2"
android:layout_height="wrap_content">
<!-- FIXME: make android:digits locale independent -->
<EditText android:id="@+id/input_exchange_rate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:enabled="false"
android:inputType="numberDecimal"
gnucash:keyboardKeysLayout="@xml/calculator_keyboard"
android:digits="0123456789.,"
android:imeOptions="actionDone"
android:hint="@string/hint_exchange_rate"/>
</android.support.design.widget.TextInputLayout>

Expand All @@ -110,6 +112,7 @@
android:layout_height="wrap_content"
android:enabled="false"
android:visibility="gone"
android:imeOptions="actionDone"
android:text="@string/btn_fetch_quote"/>
</TableRow>
<TextView android:id="@+id/label_exchange_rate_example"
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values-af-rZA/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,8 @@
<string name="toast_no_compatible_apps_to_receive_export">No compatible apps to receive the exported transactions!</string>
<string name="menu_move_transaction">Move...</string>
<string name="menu_duplicate_transaction">Duplicate</string>
<string name="error_invalid_exchange_rate">Invalid exchange rate</string>
<string name="sample_exchange_rate">e.g. 1 %1$s = x.xx %2$s</string>
<string-array name="report_time_range">
<item>Current month</item>
<item>Last 3 months</item>
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values-ar-rSA/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,8 @@
<string name="toast_no_compatible_apps_to_receive_export">No compatible apps to receive the exported transactions!</string>
<string name="menu_move_transaction">Move...</string>
<string name="menu_duplicate_transaction">Duplicate</string>
<string name="error_invalid_exchange_rate">Invalid exchange rate</string>
<string name="sample_exchange_rate">e.g. 1 %1$s = x.xx %2$s</string>
<string-array name="report_time_range">
<item>Current month</item>
<item>Last 3 months</item>
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values-ca-rES/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,8 @@
<string name="toast_no_compatible_apps_to_receive_export">No compatible apps to receive the exported transactions!</string>
<string name="menu_move_transaction">Move...</string>
<string name="menu_duplicate_transaction">Duplicate</string>
<string name="error_invalid_exchange_rate">Invalid exchange rate</string>
<string name="sample_exchange_rate">e.g. 1 %1$s = x.xx %2$s</string>
<string-array name="report_time_range">
<item>Mes actual</item>
<item>Últims 3 mesos</item>
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values-cs-rCZ/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,8 @@
<string name="toast_no_compatible_apps_to_receive_export">No compatible apps to receive the exported transactions!</string>
<string name="menu_move_transaction">Move...</string>
<string name="menu_duplicate_transaction">Duplicate</string>
<string name="error_invalid_exchange_rate">Invalid exchange rate</string>
<string name="sample_exchange_rate">e.g. 1 %1$s = x.xx %2$s</string>
<string-array name="report_time_range">
<item>Current month</item>
<item>Last 3 months</item>
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values-de/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,8 @@ No user-identifiable information will be collected as part of this process!</str
<string name="toast_no_compatible_apps_to_receive_export">Es gibt keine kompatible App, welche die exportierten Buchungen empfangen kann!</string>
<string name="menu_move_transaction">Verschieben…</string>
<string name="menu_duplicate_transaction">Kopieren</string>
<string name="error_invalid_exchange_rate">Invalid exchange rate</string>
<string name="sample_exchange_rate">e.g. 1 %1$s = x.xx %2$s</string>
<string-array name="report_time_range">
<item>Aktueller Monat</item>
<item>Letzte 3 Monate</item>
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values-el-rGR/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,8 @@ No user-identifiable information will be collected as part of this process!
<string name="toast_no_compatible_apps_to_receive_export">No compatible apps to receive the exported transactions!</string>
<string name="menu_move_transaction">Move...</string>
<string name="menu_duplicate_transaction">Duplicate</string>
<string name="error_invalid_exchange_rate">Invalid exchange rate</string>
<string name="sample_exchange_rate">e.g. 1 %1$s = x.xx %2$s</string>
<string-array name="report_time_range">
<item>Current month</item>
<item>Last 3 months</item>
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values-en-rGB/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,8 @@
<string name="toast_no_compatible_apps_to_receive_export">No compatible apps to receive the exported transactions!</string>
<string name="menu_move_transaction">Move...</string>
<string name="menu_duplicate_transaction">Duplicate</string>
<string name="error_invalid_exchange_rate">Invalid exchange rate</string>
<string name="sample_exchange_rate">e.g. 1 %1$s = x.xx %2$s</string>
<string-array name="report_time_range">
<item>Current month</item>
<item>Last 3 months</item>
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values-es-rMX/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,8 @@ Este proceso solo recoge información que no permite identificar al usuario</str
<string name="toast_no_compatible_apps_to_receive_export">No compatible apps to receive the exported transactions!</string>
<string name="menu_move_transaction">Move...</string>
<string name="menu_duplicate_transaction">Duplicate</string>
<string name="error_invalid_exchange_rate">Invalid exchange rate</string>
<string name="sample_exchange_rate">e.g. 1 %1$s = x.xx %2$s</string>
<string-array name="report_time_range">
<item>Mes actual</item>
<item>Últimos 3 meses</item>
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values-es/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,8 @@ Este proceso solo recoge información que no permite identificar al usuario</str
<string name="toast_no_compatible_apps_to_receive_export">¡No hay aplicaciones compatibles para recibir las transacciones exportadas!</string>
<string name="menu_move_transaction">Mover...</string>
<string name="menu_duplicate_transaction">Duplicar</string>
<string name="error_invalid_exchange_rate">Invalid exchange rate</string>
<string name="sample_exchange_rate">e.g. 1 %1$s = x.xx %2$s</string>
<string-array name="report_time_range">
<item>Mes actual</item>
<item>Últimos 3 meses</item>
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values-fi-rFI/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,8 @@
<string name="toast_no_compatible_apps_to_receive_export">No compatible apps to receive the exported transactions!</string>
<string name="menu_move_transaction">Move...</string>
<string name="menu_duplicate_transaction">Duplicate</string>
<string name="error_invalid_exchange_rate">Invalid exchange rate</string>
<string name="sample_exchange_rate">e.g. 1 %1$s = x.xx %2$s</string>
<string-array name="report_time_range">
<item>Current month</item>
<item>Last 3 months</item>
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values-fr/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,8 @@ Aucune information permettant d\'identifier l\'utilisateur ne sera recueillis da
<string name="toast_no_compatible_apps_to_receive_export">No compatible apps to receive the exported transactions!</string>
<string name="menu_move_transaction">Move...</string>
<string name="menu_duplicate_transaction">Duplicate</string>
<string name="error_invalid_exchange_rate">Invalid exchange rate</string>
<string name="sample_exchange_rate">e.g. 1 %1$s = x.xx %2$s</string>
<string-array name="report_time_range">
<item>Mois actuel</item>
<item>3 derniers mois</item>
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values-hu-rHU/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,8 @@ No user-identifiable information will be collected as part of this process!
<string name="toast_no_compatible_apps_to_receive_export">No compatible apps to receive the exported transactions!</string>
<string name="menu_move_transaction">Move...</string>
<string name="menu_duplicate_transaction">Duplicate</string>
<string name="error_invalid_exchange_rate">Invalid exchange rate</string>
<string name="sample_exchange_rate">e.g. 1 %1$s = x.xx %2$s</string>
<string-array name="report_time_range">
<item>Current month</item>
<item>Last 3 months</item>
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values-it-rIT/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,8 @@
<string name="toast_no_compatible_apps_to_receive_export">Nessuna app compatibile con la ricezione delle transazioni esportate!</string>
<string name="menu_move_transaction">Sposta...</string>
<string name="menu_duplicate_transaction">Duplica</string>
<string name="error_invalid_exchange_rate">Invalid exchange rate</string>
<string name="sample_exchange_rate">e.g. 1 %1$s = x.xx %2$s</string>
<string-array name="report_time_range">
<item>Mese corrente</item>
<item>Ultimi 3 mesi</item>
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values-iw-rIL/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,8 @@
<string name="toast_no_compatible_apps_to_receive_export">No compatible apps to receive the exported transactions!</string>
<string name="menu_move_transaction">Move...</string>
<string name="menu_duplicate_transaction">Duplicate</string>
<string name="error_invalid_exchange_rate">Invalid exchange rate</string>
<string name="sample_exchange_rate">e.g. 1 %1$s = x.xx %2$s</string>
<string-array name="report_time_range">
<item>Current month</item>
<item>Last 3 months</item>
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values-ja-rJP/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,8 @@
<string name="toast_no_compatible_apps_to_receive_export">No compatible apps to receive the exported transactions!</string>
<string name="menu_move_transaction">Move...</string>
<string name="menu_duplicate_transaction">Duplicate</string>
<string name="error_invalid_exchange_rate">Invalid exchange rate</string>
<string name="sample_exchange_rate">e.g. 1 %1$s = x.xx %2$s</string>
<string-array name="report_time_range">
<item>Current month</item>
<item>Last 3 months</item>
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values-ko-rKR/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,8 @@
<string name="toast_no_compatible_apps_to_receive_export">No compatible apps to receive the exported transactions!</string>
<string name="menu_move_transaction">Move...</string>
<string name="menu_duplicate_transaction">Duplicate</string>
<string name="error_invalid_exchange_rate">Invalid exchange rate</string>
<string name="sample_exchange_rate">e.g. 1 %1$s = x.xx %2$s</string>
<string-array name="report_time_range">
<item>Current month</item>
<item>Last 3 months</item>
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values-lv-rLV/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,8 @@
<string name="toast_no_compatible_apps_to_receive_export">No compatible apps to receive the exported transactions!</string>
<string name="menu_move_transaction">Move...</string>
<string name="menu_duplicate_transaction">Duplicate</string>
<string name="error_invalid_exchange_rate">Invalid exchange rate</string>
<string name="sample_exchange_rate">e.g. 1 %1$s = x.xx %2$s</string>
<string-array name="report_time_range">
<item>Current month</item>
<item>Last 3 months</item>
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values-nb/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,8 @@
<string name="toast_no_compatible_apps_to_receive_export">Ingen kompatible apper til å motta de eksporterte transaksjonene!</string>
<string name="menu_move_transaction">Move...</string>
<string name="menu_duplicate_transaction">Duplicate</string>
<string name="error_invalid_exchange_rate">Invalid exchange rate</string>
<string name="sample_exchange_rate">e.g. 1 %1$s = x.xx %2$s</string>
<string-array name="report_time_range">
<item>Gjeldende måned</item>
<item>Siste 3 måneder</item>
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values-nl-rNL/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,8 @@ No user-identifiable information will be collected as part of this process!
<string name="toast_no_compatible_apps_to_receive_export">No compatible apps to receive the exported transactions!</string>
<string name="menu_move_transaction">Move...</string>
<string name="menu_duplicate_transaction">Duplicate</string>
<string name="error_invalid_exchange_rate">Invalid exchange rate</string>
<string name="sample_exchange_rate">e.g. 1 %1$s = x.xx %2$s</string>
<string-array name="report_time_range">
<item>Huidige maand</item>
<item>Afgelopen 3 maanden</item>
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values-no-rNO/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,8 @@
<string name="toast_no_compatible_apps_to_receive_export">Ingen kompatible apper til å motta de eksporterte transaksjonene!</string>
<string name="menu_move_transaction">Move...</string>
<string name="menu_duplicate_transaction">Duplicate</string>
<string name="error_invalid_exchange_rate">Invalid exchange rate</string>
<string name="sample_exchange_rate">e.g. 1 %1$s = x.xx %2$s</string>
<string-array name="report_time_range">
<item>Gjeldende måned</item>
<item>Siste 3 måneder</item>
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values-pl-rPL/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,8 @@
<string name="toast_no_compatible_apps_to_receive_export">No compatible apps to receive the exported transactions!</string>
<string name="menu_move_transaction">Move...</string>
<string name="menu_duplicate_transaction">Duplicate</string>
<string name="error_invalid_exchange_rate">Invalid exchange rate</string>
<string name="sample_exchange_rate">e.g. 1 %1$s = x.xx %2$s</string>
<string-array name="report_time_range">
<item>Bieżący miesiąc</item>
<item>Ostatnie 3 miesiące</item>
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values-pt-rBR/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,8 @@ Neste processo não serão recolhidas informações do utilizador!</string>
<string name="toast_no_compatible_apps_to_receive_export">Não existem apps compatíveis para receber as transações exportadas!</string>
<string name="menu_move_transaction">Mover...</string>
<string name="menu_duplicate_transaction">Duplicar</string>
<string name="error_invalid_exchange_rate">Invalid exchange rate</string>
<string name="sample_exchange_rate">e.g. 1 %1$s = x.xx %2$s</string>
<string-array name="report_time_range">
<item>Mês atual</item>
<item>Last 3 meses</item>
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values-pt-rPT/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,8 @@ Neste processo não serão recolhidas informações do utilizador!</string>
<string name="toast_no_compatible_apps_to_receive_export">Não existem aplicações compatíveis para receber as transações exportadas!</string>
<string name="menu_move_transaction">Mover...</string>
<string name="menu_duplicate_transaction">Duplicar</string>
<string name="error_invalid_exchange_rate">Invalid exchange rate</string>
<string name="sample_exchange_rate">e.g. 1 %1$s = x.xx %2$s</string>
<string-array name="report_time_range">
<item>Mês actual</item>
<item>Últimos 3 meses</item>
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values-ro-rRO/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,8 @@
<string name="toast_no_compatible_apps_to_receive_export">No compatible apps to receive the exported transactions!</string>
<string name="menu_move_transaction">Move...</string>
<string name="menu_duplicate_transaction">Duplicate</string>
<string name="error_invalid_exchange_rate">Invalid exchange rate</string>
<string name="sample_exchange_rate">e.g. 1 %1$s = x.xx %2$s</string>
<string-array name="report_time_range">
<item>Current month</item>
<item>Last 3 months</item>
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values-ru/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,8 @@
<string name="toast_no_compatible_apps_to_receive_export">Не совместимых приложений, чтобы получить экспортированные транзакции!</string>
<string name="menu_move_transaction">Переместить...</string>
<string name="menu_duplicate_transaction">Повтор</string>
<string name="error_invalid_exchange_rate">Invalid exchange rate</string>
<string name="sample_exchange_rate">e.g. 1 %1$s = x.xx %2$s</string>
<string-array name="report_time_range">
<item>Текущий месяц</item>
<item>Последний квартал</item>
Expand Down
Loading

0 comments on commit e7ee18e

Please sign in to comment.