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
@@ -486,8 +486,8 @@ abstract class AbstractFileTrackImporter extends DefaultHandler implements Track
latitudeValue = Double.parseDouble(latitude);
longitudeValue = Double.parseDouble(longitude);
} catch (NumberFormatException e) {
throw new SAXException(createErrorMessage(
String.format("Unable to parse latitude longitude: %s %s", latitude, longitude)), e);
throw new SAXException(createErrorMessage(String.format(
Locale.US, "Unable to parse latitude longitude: %s %s", latitude, longitude)), e);
}
Double altitudeValue = null;
if (altitude != null) {
@@ -495,7 +495,8 @@ abstract class AbstractFileTrackImporter extends DefaultHandler implements Track
altitudeValue = Double.parseDouble(altitude);
} catch (NumberFormatException e) {
throw new SAXException(
createErrorMessage(String.format("Unable to parse altitude: %s", altitude)), e);
createErrorMessage(String.format(Locale.US, "Unable to parse altitude: %s", altitude)),
e);
}
}
@@ -507,7 +508,7 @@ abstract class AbstractFileTrackImporter extends DefaultHandler implements Track
timeValue = StringUtils.getTime(time);
} catch (IllegalArgumentException e) {
throw new SAXException(
createErrorMessage(String.format("Unable to parse time: %s", time)), e);
createErrorMessage(String.format(Locale.US, "Unable to parse time: %s", time)), e);
}
}
return createLocation(latitudeValue, longitudeValue, altitudeValue, timeValue);
@@ -38,6 +38,7 @@ import android.util.Log;
import android.widget.Toast;
import java.lang.reflect.Field;
import java.util.Locale;
/**
* Ant Sensor Manager.
@@ -396,7 +397,8 @@ public class AntSensorManager extends SensorManager {
if (channelConfig[channel].isInitializing()) {
if (message[AntMesg.MESG_DATA_OFFSET + 2] != 0) {
// Error response
Log.e(TAG, String.format("Error code(%#02x) on message ID(%#02x) on channel %d",
Log.e(TAG, String.format(Locale.US,
"Error code(%#02x) on message ID(%#02x) on channel %d",
message[AntMesg.MESG_DATA_OFFSET + 2], message[AntMesg.MESG_DATA_OFFSET + 1],
channel));
} else {
@@ -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;
}