Number formatting

This commit is contained in:
serso 2016-02-11 23:39:34 +01:00
parent fc48886929
commit 52cc460451
2 changed files with 14 additions and 3 deletions

View File

@ -1,6 +1,7 @@
package org.solovyev.android.calculator; package org.solovyev.android.calculator;
import dagger.Component; import dagger.Component;
import org.solovyev.android.calculator.converter.ConverterFragment;
import org.solovyev.android.calculator.errors.FixableErrorFragment; import org.solovyev.android.calculator.errors.FixableErrorFragment;
import org.solovyev.android.calculator.errors.FixableErrorsActivity; import org.solovyev.android.calculator.errors.FixableErrorsActivity;
import org.solovyev.android.calculator.functions.EditFunctionFragment; import org.solovyev.android.calculator.functions.EditFunctionFragment;
@ -35,6 +36,7 @@ public interface AppComponent {
void inject(FunctionsFragment fragment); void inject(FunctionsFragment fragment);
void inject(VariablesFragment fragment); void inject(VariablesFragment fragment);
void inject(OperatorsFragment fragment); void inject(OperatorsFragment fragment);
void inject(ConverterFragment fragment);
void inject(CalculatorActivity activity); void inject(CalculatorActivity activity);
void inject(FixableErrorsActivity activity); void inject(FixableErrorsActivity activity);
void inject(CalculatorReceiver receiver); void inject(CalculatorReceiver receiver);

View File

@ -19,6 +19,7 @@ import android.widget.*;
import butterknife.Bind; import butterknife.Bind;
import butterknife.ButterKnife; import butterknife.ButterKnife;
import org.solovyev.android.calculator.App; import org.solovyev.android.calculator.App;
import org.solovyev.android.calculator.AppComponent;
import org.solovyev.android.calculator.BaseDialogFragment; import org.solovyev.android.calculator.BaseDialogFragment;
import org.solovyev.android.calculator.R; import org.solovyev.android.calculator.R;
@ -27,11 +28,15 @@ import javax.measure.unit.Dimension;
import javax.measure.unit.NonSI; import javax.measure.unit.NonSI;
import javax.measure.unit.SI; import javax.measure.unit.SI;
import javax.measure.unit.Unit; import javax.measure.unit.Unit;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.util.*; import java.util.*;
public class ConverterFragment extends BaseDialogFragment public class ConverterFragment extends BaseDialogFragment
implements AdapterView.OnItemSelectedListener, View.OnFocusChangeListener, TextView.OnEditorActionListener, View.OnClickListener, TextWatcher { implements AdapterView.OnItemSelectedListener, View.OnFocusChangeListener, TextView.OnEditorActionListener, View.OnClickListener, TextWatcher {
@Nonnull
private static final DecimalFormat formatter = new DecimalFormat("0.#####E0");
@NonNull @NonNull
private static final Set<String> excludedUnits = new HashSet<>(Arrays.asList("year_sidereal", "year_calendar", "day_sidereal", "foot_survey_us")); private static final Set<String> excludedUnits = new HashSet<>(Arrays.asList("year_sidereal", "year_calendar", "day_sidereal", "foot_survey_us"));
@NonNull @NonNull
@ -113,7 +118,11 @@ public class ConverterFragment extends BaseDialogFragment
@Override @Override
protected void onPrepareDialog(@NonNull AlertDialog.Builder builder) { protected void onPrepareDialog(@NonNull AlertDialog.Builder builder) {
}
@Override
protected void inject(@NonNull AppComponent component) {
super.inject(component);
} }
@SuppressLint("InflateParams") @SuppressLint("InflateParams")
@ -264,13 +273,13 @@ public class ConverterFragment extends BaseDialogFragment
} }
try { try {
final Double fromValue = Double.valueOf(value); final Double fromValue = formatter.parse(value).doubleValue();
final Unit<?> from = adapterFrom.getItem(spinnerFrom.getSelectedItemPosition()); final Unit<?> from = adapterFrom.getItem(spinnerFrom.getSelectedItemPosition());
final Unit<?> to = adapterTo.getItem(spinnerTo.getSelectedItemPosition()); final Unit<?> to = adapterTo.getItem(spinnerTo.getSelectedItemPosition());
final double toValue = from.getConverterTo(to).convert(fromValue); final double toValue = from.getConverterTo(to).convert(fromValue);
editTextTo.setText(String.valueOf(toValue)); editTextTo.setText(formatter.format(toValue));
clearError(labelFrom); clearError(labelFrom);
} catch (RuntimeException e) { } catch (RuntimeException | ParseException e) {
if (validate) { if (validate) {
setError(labelFrom, e.getLocalizedMessage()); setError(labelFrom, e.getLocalizedMessage());
} }