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_9);
press(R.id.btn_0);
checkResult("1234567890");
checkResult("1,234,567,890");
}
@Test
@ -141,6 +141,8 @@ public class MainActivityTest {
checkResult("2");
press(R.id.btn_clear);
checkResult("0");
press(R.id.btn_clear);
checkResult("0");
}
@Test

View File

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

View File

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