implement decimals and correct handling of 0

This commit is contained in:
tibbi 2015-12-24 16:47:18 +01:00
parent c84200a101
commit 576520dc3a
1 changed files with 23 additions and 6 deletions

View File

@ -5,6 +5,8 @@ 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;
@ -26,23 +28,38 @@ public class MainActivity extends AppCompatActivity {
} }
private String getFormattedValue(String str) { private String getFormattedValue(String str) {
return formatDouble(Double.parseDouble(str)); double doubleValue = Double.parseDouble(str);
return formatDouble(doubleValue);
} }
private String formatDouble(double d) { private String formatDouble(double d) {
if (d == (long) d) { if (d == (long) d) {
return String.format("%d", (long) d); return String.format("%d", (long) d);
} else { } else {
return String.format("%s", d); final DecimalFormat formatter = new DecimalFormat("0.0############");
return formatter.format(d);
} }
} }
@OnClick({R.id.btn_0, R.id.btn_1, R.id.btn_2, R.id.btn_3, R.id.btn_4, R.id.btn_5, R.id.btn_6, R.id.btn_7, R.id.btn_8, R.id.btn_9}) @OnClick(R.id.btn_decimal)
public void decimalClicked() {
String value = result.getText().toString();
if (!value.contains("."))
value += ".";
result.setText(value);
}
@OnClick(R.id.btn_0)
public void zeroClicked() {
String value = result.getText().toString();
if (!value.isEmpty() && !value.equals("0"))
value += "0";
result.setText(value);
}
@OnClick({R.id.btn_1, R.id.btn_2, R.id.btn_3, R.id.btn_4, R.id.btn_5, R.id.btn_6, R.id.btn_7, R.id.btn_8, R.id.btn_9})
public void digitClicked(View view) { public void digitClicked(View view) {
switch (view.getId()) { switch (view.getId()) {
case R.id.btn_0:
addDigit(0);
break;
case R.id.btn_1: case R.id.btn_1:
addDigit(1); addDigit(1);
break; break;