stack formatter: support larger stack sizes
This commit is contained in:
@@ -58,12 +58,12 @@ public class StackFormatter
|
||||
* @return A condensed version, with commas, K, M or B
|
||||
* as needed to 3 significant figures.
|
||||
*/
|
||||
public static String quantityToStackSize(int quantity)
|
||||
public static String quantityToStackSize(long quantity)
|
||||
{
|
||||
if (quantity < 0)
|
||||
{
|
||||
// Integer.MIN_VALUE = -1 * Integer.MIN_VALUE so we need to correct for it.
|
||||
return "-" + quantityToStackSize(quantity == Integer.MIN_VALUE ? Integer.MAX_VALUE : -quantity);
|
||||
// Long.MIN_VALUE = -1 * Long.MIN_VALUE so we need to correct for it.
|
||||
return "-" + quantityToStackSize(quantity == Long.MIN_VALUE ? Long.MAX_VALUE : -quantity);
|
||||
}
|
||||
else if (quantity < 10_000)
|
||||
{
|
||||
@@ -71,14 +71,14 @@ public class StackFormatter
|
||||
}
|
||||
|
||||
String suffix = SUFFIXES[0];
|
||||
int divideBy = 1;
|
||||
long divideBy = 1;
|
||||
|
||||
// determine correct suffix by iterating backward through the list
|
||||
// of suffixes until the suffix results in a value >= 1
|
||||
for (int i = (SUFFIXES.length - 1); i >= 0; i--)
|
||||
{
|
||||
divideBy = (int) Math.pow(10, i * 3);
|
||||
if ((float) quantity / divideBy >= 1)
|
||||
divideBy = (long) Math.pow(10, i * 3);
|
||||
if ((double) quantity / divideBy >= 1)
|
||||
{
|
||||
suffix = SUFFIXES[i];
|
||||
break;
|
||||
@@ -86,7 +86,7 @@ public class StackFormatter
|
||||
}
|
||||
|
||||
// get locale formatted string
|
||||
String formattedString = NUMBER_FORMATTER.format((float) quantity / divideBy);
|
||||
String formattedString = NUMBER_FORMATTER.format((double) quantity / divideBy);
|
||||
|
||||
// strip down any digits past the 4 first
|
||||
formattedString = (formattedString.length() > 4 ? formattedString.substring(0, 4) : formattedString);
|
||||
|
||||
@@ -61,12 +61,14 @@ public class StackFormatterTest
|
||||
assertEquals("100K", StackFormatter.quantityToStackSize(100_000));
|
||||
assertEquals("10M", StackFormatter.quantityToStackSize(10_000_000));
|
||||
assertEquals(NumberFormat.getNumberInstance().format(2.14) + "B", StackFormatter.quantityToStackSize(Integer.MAX_VALUE));
|
||||
assertEquals("100B", StackFormatter.quantityToStackSize(100_000_000_000L));
|
||||
|
||||
assertEquals("0", StackFormatter.quantityToStackSize(-0));
|
||||
assertEquals("-400", StackFormatter.quantityToStackSize(-400));
|
||||
assertEquals("-400K", StackFormatter.quantityToStackSize(-400_000));
|
||||
assertEquals("-40M", StackFormatter.quantityToStackSize(-40_000_000));
|
||||
assertEquals(NumberFormat.getNumberInstance().format(-2.14) + "B", StackFormatter.quantityToStackSize(Integer.MIN_VALUE));
|
||||
assertEquals("-400B", StackFormatter.quantityToStackSize(-400_000_000_000L));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -103,4 +105,4 @@ public class StackFormatterTest
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user