mirror of
https://codeberg.org/OpenTracksApp/OpenTracks.git
synced 2026-10-02 17:43:06 +02:00
Present speed only with one fractional digit.
This commit is contained in:
@@ -202,7 +202,7 @@ public class StringUtilsTest {
|
||||
assertEquals("1", StringUtils.formatDecimal(1.1, 0));
|
||||
assertEquals("10", StringUtils.formatDecimal(10, 0));
|
||||
assertEquals("10", StringUtils.formatDecimal(10.1, 0));
|
||||
assertEquals("0", StringUtils.formatDecimal(-0.1, 0));
|
||||
assertEquals("-0", StringUtils.formatDecimal(-0.1, 0));
|
||||
|
||||
assertEquals("0", StringUtils.formatDecimal(0.0, 2));
|
||||
assertEquals("0.1", StringUtils.formatDecimal(0.1, 2));
|
||||
@@ -220,8 +220,8 @@ public class StringUtilsTest {
|
||||
assertEquals("4:59", StringUtils.getSpeedParts(context, 3.34, true, false).first);
|
||||
assertEquals("5:00", StringUtils.getSpeedParts(context, 3.33, true, false).first);
|
||||
|
||||
assertEquals("11.99", StringUtils.getSpeedParts(context, 3.33, true, true).first);
|
||||
assertEquals("7.45", StringUtils.getSpeedParts(context, 3.33, false, true).first);
|
||||
assertEquals("11.9", StringUtils.getSpeedParts(context, 3.31, true, true).first);
|
||||
assertEquals("7.5", StringUtils.getSpeedParts(context, 3.34, false, true).first);
|
||||
|
||||
assertEquals("min/km", StringUtils.getSpeedParts(context, 0, true, false).second);
|
||||
assertEquals("min/mi", StringUtils.getSpeedParts(context, 0, false, false).second);
|
||||
|
||||
@@ -22,6 +22,7 @@ import android.text.format.DateUtils;
|
||||
import android.util.Log;
|
||||
import android.util.Pair;
|
||||
|
||||
import java.math.RoundingMode;
|
||||
import java.text.DecimalFormat;
|
||||
import java.time.Instant;
|
||||
import java.time.LocalDateTime;
|
||||
@@ -125,12 +126,10 @@ public class StringUtils {
|
||||
* Format a decimal number while removing trailing zeros of the decimal part (if present).
|
||||
*/
|
||||
public static String formatDecimal(double value, int decimalPlaces) {
|
||||
if (decimalPlaces < 1) {
|
||||
return Long.toString(Math.round(value));
|
||||
}
|
||||
|
||||
String format = "#." + new String(new char[decimalPlaces]).replace("\0", "#");
|
||||
return new DecimalFormat(format).format(value);
|
||||
DecimalFormat df = new DecimalFormat();
|
||||
df.setMaximumFractionDigits(decimalPlaces);
|
||||
df.setRoundingMode(RoundingMode.HALF_EVEN);
|
||||
return df.format(value);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -204,7 +203,7 @@ public class StringUtils {
|
||||
}
|
||||
|
||||
if (reportSpeed) {
|
||||
return new Pair<>(StringUtils.formatDecimal(speed * UnitConversions.S_TO_HR), unitString);
|
||||
return new Pair<>(StringUtils.formatDecimal(speed * UnitConversions.S_TO_HR, 1), unitString);
|
||||
}
|
||||
|
||||
int pace = speed == 0 ? 0 : (int) Math.round(1 / speed); //sec / [KM | MI]
|
||||
|
||||
Reference in New Issue
Block a user