add a comma as group separator per 3 digits

This commit is contained in:
tibbi 2016-01-08 21:06:12 +01:00
parent 24d6dfbfac
commit 6578a6a02d
3 changed files with 23 additions and 19 deletions

View File

@ -30,7 +30,7 @@ public class MainActivityTest {
press(R.id.btn_8); press(R.id.btn_8);
press(R.id.btn_9); press(R.id.btn_9);
press(R.id.btn_0); press(R.id.btn_0);
checkResult("1234567890"); checkResult("1,234,567,890");
} }
@Test @Test
@ -141,6 +141,8 @@ public class MainActivityTest {
checkResult("2"); checkResult("2");
press(R.id.btn_clear); press(R.id.btn_clear);
checkResult("0"); checkResult("0");
press(R.id.btn_clear);
checkResult("0");
} }
@Test @Test

View File

@ -71,12 +71,12 @@ public class CalculatorImpl {
public void addDigit(int number) { public void addDigit(int number) {
final String currentValue = getDisplayedNumber(); final String currentValue = getDisplayedNumber();
final String newValue = removeLeadingZero(currentValue + number); final String newValue = formatString(currentValue + number);
setValue(newValue); setValue(newValue);
} }
private String removeLeadingZero(String str) { private String formatString(String str) {
final double doubleValue = Double.parseDouble(str); final double doubleValue = Formatter.stringToDouble(str);
return Formatter.doubleToString(doubleValue); return Formatter.doubleToString(doubleValue);
} }
@ -90,7 +90,7 @@ public class CalculatorImpl {
} }
public double getDisplayedNumberAsDouble() { public double getDisplayedNumberAsDouble() {
return Double.parseDouble(getDisplayedNumber()); return Formatter.stringToDouble(getDisplayedNumber());
} }
public String getDisplayedFormula() { public String getDisplayedFormula() {
@ -186,8 +186,9 @@ public class CalculatorImpl {
if (newValue.equals("-0")) if (newValue.equals("-0"))
newValue = "0"; newValue = "0";
newValue = newValue.replaceAll(",$", "");
setValue(newValue); setValue(newValue);
baseValue = Double.parseDouble(newValue); baseValue = Formatter.stringToDouble(newValue);
} }
public void handleReset() { public void handleReset() {
@ -218,8 +219,7 @@ public class CalculatorImpl {
public void zeroClicked() { public void zeroClicked() {
String value = getDisplayedNumber(); String value = getDisplayedNumber();
if (!value.equals("0")) if (!value.equals("0"))
value += "0"; addDigit(0);
setValue(value);
} }
private String getSign(String lastOperation) { private String getSign(String lastOperation) {

View File

@ -6,17 +6,19 @@ import java.util.Locale;
public class Formatter { public class Formatter {
public static String doubleToString(double d) { public static String doubleToString(double d) {
if (d == (long) d) { final DecimalFormatSymbols symbols = new DecimalFormatSymbols(Locale.US);
return String.format("%d", (long) d); symbols.setDecimalSeparator('.');
} else { symbols.setGroupingSeparator(',');
final DecimalFormatSymbols symbols = new DecimalFormatSymbols(Locale.US);
symbols.setDecimalSeparator('.');
final DecimalFormat formatter = new DecimalFormat(); final DecimalFormat formatter = new DecimalFormat();
formatter.setMaximumFractionDigits(12); formatter.setMaximumFractionDigits(12);
formatter.setDecimalFormatSymbols(symbols); formatter.setDecimalFormatSymbols(symbols);
formatter.setGroupingUsed(false); formatter.setGroupingUsed(true);
return formatter.format(d); return formatter.format(d);
} }
public static Double stringToDouble(String str) {
str = str.replaceAll(",", "");
return Double.parseDouble(str);
} }
} }