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); latitudeValue = Double.parseDouble(latitude);
longitudeValue = Double.parseDouble(longitude); longitudeValue = Double.parseDouble(longitude);
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
throw new SAXException(createErrorMessage( throw new SAXException(createErrorMessage(String.format(
String.format("Unable to parse latitude longitude: %s %s", latitude, longitude)), e); Locale.US, "Unable to parse latitude longitude: %s %s", latitude, longitude)), e);
} }
Double altitudeValue = null; Double altitudeValue = null;
if (altitude != null) { if (altitude != null) {
@@ -495,7 +495,8 @@ abstract class AbstractFileTrackImporter extends DefaultHandler implements Track
altitudeValue = Double.parseDouble(altitude); altitudeValue = Double.parseDouble(altitude);
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
throw new SAXException( 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); timeValue = StringUtils.getTime(time);
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
throw new SAXException( 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); return createLocation(latitudeValue, longitudeValue, altitudeValue, timeValue);
@@ -38,6 +38,7 @@ import android.util.Log;
import android.widget.Toast; import android.widget.Toast;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.util.Locale;
/** /**
* Ant Sensor Manager. * Ant Sensor Manager.
@@ -396,7 +397,8 @@ public class AntSensorManager extends SensorManager {
if (channelConfig[channel].isInitializing()) { if (channelConfig[channel].isInitializing()) {
if (message[AntMesg.MESG_DATA_OFFSET + 2] != 0) { if (message[AntMesg.MESG_DATA_OFFSET + 2] != 0) {
// Error response // 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], message[AntMesg.MESG_DATA_OFFSET + 2], message[AntMesg.MESG_DATA_OFFSET + 1],
channel)); channel));
} else { } else {
@@ -40,9 +40,8 @@ public class StatsUtils {
private static final String COORDINATE_DEGREE = "\u00B0"; private static final String COORDINATE_DEGREE = "\u00B0";
private static final String GRADE_PERCENTAGE = "%"; 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 GRADE_FORMAT = "%1$d";
private static final String CALORIES_FORMAT = "%1$.0f"; private static final String CALORIES_FORMAT = "%1$,.0f";
private StatsUtils() {} private StatsUtils() {}
@@ -282,11 +281,11 @@ public class StatsUtils {
unit = null; unit = null;
} else { } else {
if (metricUnits) { if (metricUnits) {
value = String.format(Locale.getDefault(), ELEVATION_FORMAT, elevation); value = StringUtils.formatDecimal(elevation);
unit = context.getString(R.string.unit_meter); unit = context.getString(R.string.unit_meter);
} else { } else {
elevation *= UnitConversions.M_TO_FT; elevation *= UnitConversions.M_TO_FT;
value = String.format(Locale.getDefault(), ELEVATION_FORMAT, elevation); value = StringUtils.formatDecimal(elevation);
unit = context.getString(R.string.unit_feet); 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 * 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; unitId = R.string.unit_feet;
} }
} }
result[0] = String.format("%.2f", distance); result[0] = formatDecimal(distance);
result[1] = context.getString(unitId); result[1] = context.getString(unitId);
return result; return result;
} }
@@ -217,13 +222,13 @@ public class StringUtils {
speed *= UnitConversions.KM_TO_MI; speed *= UnitConversions.KM_TO_MI;
} }
if (reportSpeed) { if (reportSpeed) {
result[0] = String.format("%.2f", speed); result[0] = StringUtils.formatDecimal(speed);
} else { } else {
// convert from hours to minutes // convert from hours to minutes
double pace = speed == 0 ? 0.0 : 60.0 / speed; double pace = speed == 0 ? 0.0 : 60.0 / speed;
int minutes = (int) pace; int minutes = (int) pace;
int seconds = (int) Math.round((pace - minutes) * 60.0); 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; return result;
} }