From 271bf692dd8b51fa18767430d4b5c1f608178fc0 Mon Sep 17 00:00:00 2001 From: tibbi Date: Thu, 24 Dec 2015 21:00:58 +0100 Subject: [PATCH] move the formatter in a separate class --- .../com/simple_calculator/Formatter.java | 14 ++++++++++++++ .../com/simple_calculator/MainActivity.java | 15 ++------------- 2 files changed, 16 insertions(+), 13 deletions(-) create mode 100644 app/src/main/java/calculator/simplemobiletools/com/simple_calculator/Formatter.java diff --git a/app/src/main/java/calculator/simplemobiletools/com/simple_calculator/Formatter.java b/app/src/main/java/calculator/simplemobiletools/com/simple_calculator/Formatter.java new file mode 100644 index 00000000..99d7bad0 --- /dev/null +++ b/app/src/main/java/calculator/simplemobiletools/com/simple_calculator/Formatter.java @@ -0,0 +1,14 @@ +package calculator.simplemobiletools.com.simple_calculator; + +import java.text.DecimalFormat; + +public class Formatter { + public static String doubleToString(double d) { + if (d == (long) d) { + return String.format("%d", (long) d); + } else { + final DecimalFormat formatter = new DecimalFormat("0.0#############"); + return formatter.format(d); + } + } +} diff --git a/app/src/main/java/calculator/simplemobiletools/com/simple_calculator/MainActivity.java b/app/src/main/java/calculator/simplemobiletools/com/simple_calculator/MainActivity.java index ede7e056..3764dcd9 100644 --- a/app/src/main/java/calculator/simplemobiletools/com/simple_calculator/MainActivity.java +++ b/app/src/main/java/calculator/simplemobiletools/com/simple_calculator/MainActivity.java @@ -5,8 +5,6 @@ import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.TextView; -import java.text.DecimalFormat; - import butterknife.Bind; import butterknife.ButterKnife; import butterknife.OnClick; @@ -39,16 +37,7 @@ public class MainActivity extends AppCompatActivity { private String getFormattedValue(String str) { final double doubleValue = Double.parseDouble(str); - return formatDouble(doubleValue); - } - - private String formatDouble(double d) { - if (d == (long) d) { - return String.format("%d", (long) d); - } else { - final DecimalFormat formatter = new DecimalFormat("0.0#############"); - return formatter.format(d); - } + return Formatter.doubleToString(doubleValue); } private String getDisplayedNumber() { @@ -69,7 +58,7 @@ public class MainActivity extends AppCompatActivity { private void addNumbers() { final double resultValue = firstValue + secondValue; - result.setText(formatDouble(resultValue)); + result.setText(Formatter.doubleToString(resultValue)); firstValue = resultValue; }