diff --git a/src/main/java/at/rtr/rmbt/service/impl/TestServiceImpl.java b/src/main/java/at/rtr/rmbt/service/impl/TestServiceImpl.java index 01e552a..9d3d1d3 100644 --- a/src/main/java/at/rtr/rmbt/service/impl/TestServiceImpl.java +++ b/src/main/java/at/rtr/rmbt/service/impl/TestServiceImpl.java @@ -701,11 +701,9 @@ private void addTestFields(List propertiesLis .ifPresentOrElse(mobileProviderShortName -> addNetworkOperator(propertiesList, locale, test, mobileProviderShortName), () -> addString(propertiesList, locale, "network_operator_name", test.getNetworkOperatorName())); } Optional.ofNullable(test.getDownloadSpeed()) - .map(x -> x / Constants.BYTES_UNIT_CONVERSION_MULTIPLICATOR) - .ifPresent(downloadSpeed -> addDoubleAndUnitString(propertiesList, locale, "speed_download", downloadSpeed, "RESULT_DOWNLOAD_UNIT")); + .ifPresent(downloadSpeed -> addSpeedAndUnitString(propertiesList, locale, "speed_download", downloadSpeed, "RESULT_DOWNLOAD_UNIT")); Optional.ofNullable(test.getUploadSpeed()) - .map(x -> x / Constants.BYTES_UNIT_CONVERSION_MULTIPLICATOR) - .ifPresent(uploadSpeed -> addDoubleAndUnitString(propertiesList, locale, "speed_upload", uploadSpeed, "RESULT_UPLOAD_UNIT")); + .ifPresent(uploadSpeed -> addSpeedAndUnitString(propertiesList, locale, "speed_upload", uploadSpeed, "RESULT_UPLOAD_UNIT")); Optional.ofNullable(test.getPingMedian()) .map(this::getPingMsFromPingMedian) .ifPresent(pingMedian -> addDoubleAndUnitString(propertiesList, locale, "ping_median", pingMedian, "RESULT_PING_UNIT")); @@ -933,6 +931,13 @@ private void addDoubleAndUnitString(List prop } } + private void addSpeedAndUnitString(List propertiesList, Locale locale, String title, Integer value, String unitKey) { + String unit = getStringFromBundle(unitKey, locale); + if (Objects.nonNull(value)) { + addString(propertiesList, locale, title, FormatUtils.formatSpeedValueAndUnit(value, unit, locale)); + } + } + private void addIntegerAndUnitString(List propertiesList, Locale locale, String title, Integer value, String unitKey) { String unit = getStringFromBundle(unitKey, locale); if (Objects.nonNull(value)) { diff --git a/src/main/java/at/rtr/rmbt/utils/FormatUtils.java b/src/main/java/at/rtr/rmbt/utils/FormatUtils.java index 1b2f4e3..c54fc9e 100644 --- a/src/main/java/at/rtr/rmbt/utils/FormatUtils.java +++ b/src/main/java/at/rtr/rmbt/utils/FormatUtils.java @@ -40,6 +40,11 @@ public static String formatValueAndUnit(Double value, String unitValue, Locale l return String.format(Constants.VALUE_AND_UNIT_TEMPLATE, format.format(new BigDecimal(value, mathContext)), unitValue); } + public static String formatSpeedValueAndUnit(Integer speedMbps, String unitValue, Locale locale) { + Format format = NumberFormat.getNumberInstance(locale); + return String.format(Constants.VALUE_AND_UNIT_TEMPLATE, formatSpeed(speedMbps), unitValue); + } + public static String formatValueAndUnit(Long value, String unitValue) { return String.format(Constants.VALUE_AND_UNIT_TEMPLATE, value, unitValue); } @@ -48,11 +53,18 @@ public static String formatValueAndUnit(Integer value, String unitValue) { return String.format(Constants.VALUE_AND_UNIT_TEMPLATE, value, unitValue); } - public static String formatSpeed(Integer speed) { - return Optional.ofNullable(speed) + public static String formatSpeed(Integer speedKbps) { + return Optional.ofNullable(speedKbps) .map(s -> ObjectUtils.defaultIfNull(s, NumberUtils.INTEGER_ZERO)) .map(x -> x / Constants.BYTES_UNIT_CONVERSION_MULTIPLICATOR) - .map(x -> new BigDecimal(x, mathContext)) + .map(x -> { + if (x < 100) { //don't round over 100 mbps + return new BigDecimal(x, mathContext); + } else { + return Math.round(x); + + } + }) .map(numberFormat::format) .orElse(StringUtils.EMPTY); } diff --git a/src/test/java/at/rtr/rmbt/util/FormatUtilsTest.java b/src/test/java/at/rtr/rmbt/util/FormatUtilsTest.java new file mode 100644 index 0000000..43562c3 --- /dev/null +++ b/src/test/java/at/rtr/rmbt/util/FormatUtilsTest.java @@ -0,0 +1,31 @@ +package at.rtr.rmbt.util; + +import at.rtr.rmbt.utils.FormatUtils; +import org.junit.Test; + +import static org.junit.jupiter.api.Assertions.*; + +public class FormatUtilsTest { + + @Test + public void testSpeedFormatting() { + String s = FormatUtils.formatSpeed(111); + assertEquals("0.11",s); + + s = FormatUtils.formatSpeed(1111); + assertEquals("1.1",s); + + s = FormatUtils.formatSpeed(11111); + assertEquals("11",s); + + s = FormatUtils.formatSpeed(111111); + assertEquals("111",s); + + s = FormatUtils.formatSpeed(1111111); + assertEquals("1,111",s); + + s = FormatUtils.formatSpeed(11111111); + assertEquals("11,111",s); + } + +} \ No newline at end of file