From 52cc460451920e7d37109448ef356c56c4c0d96f Mon Sep 17 00:00:00 2001 From: serso Date: Thu, 11 Feb 2016 23:39:34 +0100 Subject: [PATCH] Number formatting --- .../solovyev/android/calculator/AppComponent.java | 2 ++ .../calculator/converter/ConverterFragment.java | 15 ++++++++++++--- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/org/solovyev/android/calculator/AppComponent.java b/app/src/main/java/org/solovyev/android/calculator/AppComponent.java index 907e2083..4d27605c 100644 --- a/app/src/main/java/org/solovyev/android/calculator/AppComponent.java +++ b/app/src/main/java/org/solovyev/android/calculator/AppComponent.java @@ -1,6 +1,7 @@ package org.solovyev.android.calculator; import dagger.Component; +import org.solovyev.android.calculator.converter.ConverterFragment; import org.solovyev.android.calculator.errors.FixableErrorFragment; import org.solovyev.android.calculator.errors.FixableErrorsActivity; import org.solovyev.android.calculator.functions.EditFunctionFragment; @@ -35,6 +36,7 @@ public interface AppComponent { void inject(FunctionsFragment fragment); void inject(VariablesFragment fragment); void inject(OperatorsFragment fragment); + void inject(ConverterFragment fragment); void inject(CalculatorActivity activity); void inject(FixableErrorsActivity activity); void inject(CalculatorReceiver receiver); diff --git a/app/src/main/java/org/solovyev/android/calculator/converter/ConverterFragment.java b/app/src/main/java/org/solovyev/android/calculator/converter/ConverterFragment.java index dbf9f09d..ced53b84 100644 --- a/app/src/main/java/org/solovyev/android/calculator/converter/ConverterFragment.java +++ b/app/src/main/java/org/solovyev/android/calculator/converter/ConverterFragment.java @@ -19,6 +19,7 @@ import android.widget.*; import butterknife.Bind; import butterknife.ButterKnife; import org.solovyev.android.calculator.App; +import org.solovyev.android.calculator.AppComponent; import org.solovyev.android.calculator.BaseDialogFragment; import org.solovyev.android.calculator.R; @@ -27,11 +28,15 @@ import javax.measure.unit.Dimension; import javax.measure.unit.NonSI; import javax.measure.unit.SI; import javax.measure.unit.Unit; +import java.text.DecimalFormat; +import java.text.ParseException; import java.util.*; public class ConverterFragment extends BaseDialogFragment implements AdapterView.OnItemSelectedListener, View.OnFocusChangeListener, TextView.OnEditorActionListener, View.OnClickListener, TextWatcher { + @Nonnull + private static final DecimalFormat formatter = new DecimalFormat("0.#####E0"); @NonNull private static final Set excludedUnits = new HashSet<>(Arrays.asList("year_sidereal", "year_calendar", "day_sidereal", "foot_survey_us")); @NonNull @@ -113,7 +118,11 @@ public class ConverterFragment extends BaseDialogFragment @Override protected void onPrepareDialog(@NonNull AlertDialog.Builder builder) { + } + @Override + protected void inject(@NonNull AppComponent component) { + super.inject(component); } @SuppressLint("InflateParams") @@ -264,13 +273,13 @@ public class ConverterFragment extends BaseDialogFragment } try { - final Double fromValue = Double.valueOf(value); + final Double fromValue = formatter.parse(value).doubleValue(); final Unit from = adapterFrom.getItem(spinnerFrom.getSelectedItemPosition()); final Unit to = adapterTo.getItem(spinnerTo.getSelectedItemPosition()); final double toValue = from.getConverterTo(to).convert(fromValue); - editTextTo.setText(String.valueOf(toValue)); + editTextTo.setText(formatter.format(toValue)); clearError(labelFrom); - } catch (RuntimeException e) { + } catch (RuntimeException | ParseException e) { if (validate) { setError(labelFrom, e.getLocalizedMessage()); }