move the formatter in a separate class
This commit is contained in:
parent
eb40066bd2
commit
271bf692dd
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -5,8 +5,6 @@ import android.support.v7.app.AppCompatActivity;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
|
|
||||||
import java.text.DecimalFormat;
|
|
||||||
|
|
||||||
import butterknife.Bind;
|
import butterknife.Bind;
|
||||||
import butterknife.ButterKnife;
|
import butterknife.ButterKnife;
|
||||||
import butterknife.OnClick;
|
import butterknife.OnClick;
|
||||||
|
@ -39,16 +37,7 @@ public class MainActivity extends AppCompatActivity {
|
||||||
|
|
||||||
private String getFormattedValue(String str) {
|
private String getFormattedValue(String str) {
|
||||||
final double doubleValue = Double.parseDouble(str);
|
final double doubleValue = Double.parseDouble(str);
|
||||||
return formatDouble(doubleValue);
|
return Formatter.doubleToString(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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private String getDisplayedNumber() {
|
private String getDisplayedNumber() {
|
||||||
|
@ -69,7 +58,7 @@ public class MainActivity extends AppCompatActivity {
|
||||||
|
|
||||||
private void addNumbers() {
|
private void addNumbers() {
|
||||||
final double resultValue = firstValue + secondValue;
|
final double resultValue = firstValue + secondValue;
|
||||||
result.setText(formatDouble(resultValue));
|
result.setText(Formatter.doubleToString(resultValue));
|
||||||
firstValue = resultValue;
|
firstValue = resultValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue