Loading core/java/android/text/format/Formatter.java +7 −2 Original line number Diff line number Diff line Loading @@ -98,7 +98,8 @@ public final class Formatter { /** {@hide} */ public static BytesResult formatBytes(Resources res, long sizeBytes, int flags) { float result = sizeBytes; final boolean isNegative = (sizeBytes < 0); float result = isNegative ? -sizeBytes : sizeBytes; int suffix = com.android.internal.R.string.byteShort; long mult = 1; if (result > 900) { Loading Loading @@ -154,9 +155,13 @@ public final class Formatter { roundFormat = "%.2f"; } } if (isNegative) { result = -result; } final String roundedString = String.format(roundFormat, result); // Note this might overflow if result >= Long.MAX_VALUE / 100, but that's like 80PB so // Note this might overflow if abs(result) >= Long.MAX_VALUE / 100, but that's like 80PB so // it's okay (for now)... final long roundedBytes = (flags & FLAG_CALCULATE_ROUNDED) == 0 ? 0 Loading core/tests/coretests/src/android/text/format/FormatterTest.java +11 −9 Original line number Diff line number Diff line Loading @@ -56,14 +56,14 @@ public class FormatterTest extends AndroidTestCase { public void testFormatBytes() { setLocale(Locale.ENGLISH); checkFormatBytes(0, true, "0.00", 0); checkFormatBytes(0, false, "0.00", 0); checkFormatBytes(0, true, "0", 0); checkFormatBytes(0, false, "0", 0); checkFormatBytes(1, true, "1.0", 1); checkFormatBytes(1, false, "1.00", 1); checkFormatBytes(1, true, "1", 1); checkFormatBytes(1, false, "1", 1); checkFormatBytes(12, true, "12", 12); checkFormatBytes(12, false, "12.00", 12); checkFormatBytes(12, false, "12", 12); checkFormatBytes(123, true, "123", 123); checkFormatBytes(123, false, "123", 123); Loading @@ -80,13 +80,15 @@ public class FormatterTest extends AndroidTestCase { checkFormatBytes(9123000, true, "8.7", 9122611); checkFormatBytes(9123000, false, "8.70", 9122611); // The method doesn't really support negative values, but apparently people pass -1... checkFormatBytes(-1, true, "-1.00", -1); checkFormatBytes(-1, false, "-1.00", -1); checkFormatBytes(-1, true, "-1", -1); checkFormatBytes(-1, false, "-1", -1); checkFormatBytes(-912, true, "-0.89", -911); checkFormatBytes(-912, false, "-0.89", -911); // Missing FLAG_CALCULATE_ROUNDED case. BytesResult r = Formatter.formatBytes(getContext().getResources(), 1, 0); assertEquals("1.00", r.value); assertEquals("1", r.value); assertEquals(0, r.roundedBytes); // Didn't pass FLAG_CALCULATE_ROUNDED // Make sure it works on different locales. Loading Loading
core/java/android/text/format/Formatter.java +7 −2 Original line number Diff line number Diff line Loading @@ -98,7 +98,8 @@ public final class Formatter { /** {@hide} */ public static BytesResult formatBytes(Resources res, long sizeBytes, int flags) { float result = sizeBytes; final boolean isNegative = (sizeBytes < 0); float result = isNegative ? -sizeBytes : sizeBytes; int suffix = com.android.internal.R.string.byteShort; long mult = 1; if (result > 900) { Loading Loading @@ -154,9 +155,13 @@ public final class Formatter { roundFormat = "%.2f"; } } if (isNegative) { result = -result; } final String roundedString = String.format(roundFormat, result); // Note this might overflow if result >= Long.MAX_VALUE / 100, but that's like 80PB so // Note this might overflow if abs(result) >= Long.MAX_VALUE / 100, but that's like 80PB so // it's okay (for now)... final long roundedBytes = (flags & FLAG_CALCULATE_ROUNDED) == 0 ? 0 Loading
core/tests/coretests/src/android/text/format/FormatterTest.java +11 −9 Original line number Diff line number Diff line Loading @@ -56,14 +56,14 @@ public class FormatterTest extends AndroidTestCase { public void testFormatBytes() { setLocale(Locale.ENGLISH); checkFormatBytes(0, true, "0.00", 0); checkFormatBytes(0, false, "0.00", 0); checkFormatBytes(0, true, "0", 0); checkFormatBytes(0, false, "0", 0); checkFormatBytes(1, true, "1.0", 1); checkFormatBytes(1, false, "1.00", 1); checkFormatBytes(1, true, "1", 1); checkFormatBytes(1, false, "1", 1); checkFormatBytes(12, true, "12", 12); checkFormatBytes(12, false, "12.00", 12); checkFormatBytes(12, false, "12", 12); checkFormatBytes(123, true, "123", 123); checkFormatBytes(123, false, "123", 123); Loading @@ -80,13 +80,15 @@ public class FormatterTest extends AndroidTestCase { checkFormatBytes(9123000, true, "8.7", 9122611); checkFormatBytes(9123000, false, "8.70", 9122611); // The method doesn't really support negative values, but apparently people pass -1... checkFormatBytes(-1, true, "-1.00", -1); checkFormatBytes(-1, false, "-1.00", -1); checkFormatBytes(-1, true, "-1", -1); checkFormatBytes(-1, false, "-1", -1); checkFormatBytes(-912, true, "-0.89", -911); checkFormatBytes(-912, false, "-0.89", -911); // Missing FLAG_CALCULATE_ROUNDED case. BytesResult r = Formatter.formatBytes(getContext().getResources(), 1, 0); assertEquals("1.00", r.value); assertEquals("1", r.value); assertEquals(0, r.roundedBytes); // Didn't pass FLAG_CALCULATE_ROUNDED // Make sure it works on different locales. Loading