Improve decimal number formatting

This commit is contained in:
Jimmy Shih
2013-11-07 17:09:44 -08:00
parent 631d978ef5
commit fd63a7f5f9
4 changed files with 19 additions and 12 deletions
@@ -40,9 +40,8 @@ public class StatsUtils {
private static final String COORDINATE_DEGREE = "\u00B0";
private static final String GRADE_PERCENTAGE = "%";
private static final String ELEVATION_FORMAT = "%1$.2f";
private static final String GRADE_FORMAT = "%1$d";
private static final String CALORIES_FORMAT = "%1$.0f";
private static final String CALORIES_FORMAT = "%1$,.0f";
private StatsUtils() {}
@@ -282,11 +281,11 @@ public class StatsUtils {
unit = null;
} else {
if (metricUnits) {
value = String.format(Locale.getDefault(), ELEVATION_FORMAT, elevation);
value = StringUtils.formatDecimal(elevation);
unit = context.getString(R.string.unit_meter);
} else {
elevation *= UnitConversions.M_TO_FT;
value = String.format(Locale.getDefault(), ELEVATION_FORMAT, elevation);
value = StringUtils.formatDecimal(elevation);
unit = context.getString(R.string.unit_feet);
}
}
@@ -148,6 +148,11 @@ public class StringUtils {
}
}
}
public static String formatDecimal(double value) {
String result = String.format(Locale.getDefault(), "%1$,.2f", value);
return result.endsWith(".00") ? result.substring(0, result.lastIndexOf('.')) : result;
}
/**
* Gets the distance in an array of two strings. The first string is the
@@ -183,7 +188,7 @@ public class StringUtils {
unitId = R.string.unit_feet;
}
}
result[0] = String.format("%.2f", distance);
result[0] = formatDecimal(distance);
result[1] = context.getString(unitId);
return result;
}
@@ -217,13 +222,13 @@ public class StringUtils {
speed *= UnitConversions.KM_TO_MI;
}
if (reportSpeed) {
result[0] = String.format("%.2f", speed);
result[0] = StringUtils.formatDecimal(speed);
} else {
// convert from hours to minutes
double pace = speed == 0 ? 0.0 : 60.0 / speed;
int minutes = (int) pace;
int seconds = (int) Math.round((pace - minutes) * 60.0);
result[0] = String.format("%d:%02d", minutes, seconds);
result[0] = String.format(Locale.US, "%d:%02d", minutes, seconds);
}
return result;
}