committed by
Tomas Slusny
parent
a06974e78b
commit
b5d2da0d3f
@@ -24,6 +24,7 @@
|
||||
*/
|
||||
package net.runelite.client.util;
|
||||
|
||||
import java.text.DecimalFormat;
|
||||
import java.text.NumberFormat;
|
||||
import java.text.ParseException;
|
||||
import java.util.regex.Matcher;
|
||||
@@ -50,6 +51,11 @@ public class StackFormatter
|
||||
*/
|
||||
private static final NumberFormat NUMBER_FORMATTER = NumberFormat.getInstance();
|
||||
|
||||
/**
|
||||
* A decimal number formatter
|
||||
*/
|
||||
private static final NumberFormat DECIMAL_FORMATTER = new DecimalFormat("#,###.#");
|
||||
|
||||
/**
|
||||
* Convert a quantity to a nicely formatted stack size.
|
||||
* See the StackFormatterTest to see expected output.
|
||||
@@ -133,10 +139,8 @@ public class StackFormatter
|
||||
* appear in RuneScape. (with decimals)
|
||||
* <p>
|
||||
* This differs from quantityToRSStack in that it displays
|
||||
* decimals. Ex: 27100 is 27,1k (not 27k)
|
||||
* decimals. Ex: 27100 is 27.1k (not 27k)
|
||||
* <p>
|
||||
* This uses the NumberFormat singleton instead of the
|
||||
* NUMBER_FORMATTER variable to ensure the UK locale.
|
||||
*
|
||||
* @param quantity The quantity to convert.
|
||||
* @return The stack size as it would appear in RS, with decimals,
|
||||
@@ -144,43 +148,14 @@ public class StackFormatter
|
||||
*/
|
||||
public static String quantityToRSDecimalStack(int quantity)
|
||||
{
|
||||
String quantityStr = String.valueOf(quantity);
|
||||
if (quantityStr.length() <= 4)
|
||||
{
|
||||
return quantityStr;
|
||||
}
|
||||
|
||||
if (quantity < 10_000)
|
||||
{
|
||||
return Integer.toString(quantity);
|
||||
}
|
||||
else if (quantity < 1_000_000)
|
||||
{
|
||||
if (quantity % 1000 == 0)
|
||||
{
|
||||
return quantity / 1000 + "K";
|
||||
}
|
||||
return NUMBER_FORMATTER.format(quantity).substring(0, Integer.toString(quantity).length() - 1) + "K";
|
||||
}
|
||||
else if (quantity < 10_000_000)
|
||||
{
|
||||
if (quantity % 1_000_000 == 0)
|
||||
{
|
||||
return quantity / 1_000_000 + "M";
|
||||
}
|
||||
return NUMBER_FORMATTER.format(quantity).substring(0, Integer.toString(quantity).length() - 4) + "M";
|
||||
}
|
||||
else if (quantity < 1_000_000_000)
|
||||
{
|
||||
if (quantity % 1_000_000 == 0)
|
||||
{
|
||||
return quantity / 1_000_000 + "M";
|
||||
}
|
||||
return NUMBER_FORMATTER.format(quantity).substring(0, Integer.toString(quantity).length() - 4) + "M";
|
||||
}
|
||||
else
|
||||
{
|
||||
if (quantity % 1_000_000_000 == 0)
|
||||
{
|
||||
return quantity / 1_000_000_000 + "B";
|
||||
}
|
||||
return NUMBER_FORMATTER.format(quantity).substring(0, Integer.toString(quantity).length() - 7) + "B";
|
||||
}
|
||||
int power = (int) Math.log10(quantity);
|
||||
return DECIMAL_FORMATTER.format(quantity / (Math.pow(10, (power / 3) * 3))) + SUFFIXES[power / 3];
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -46,18 +46,18 @@ public class StackFormatterTest
|
||||
assertEquals("0", StackFormatter.quantityToRSDecimalStack(0));
|
||||
assertEquals("8500", StackFormatter.quantityToRSDecimalStack(8_500));
|
||||
assertEquals("10K", StackFormatter.quantityToRSDecimalStack(10_000));
|
||||
assertEquals("21,7K", StackFormatter.quantityToRSDecimalStack(21_700));
|
||||
assertEquals("21.7K", StackFormatter.quantityToRSDecimalStack(21_700));
|
||||
assertEquals("100K", StackFormatter.quantityToRSDecimalStack(100_000));
|
||||
assertEquals("100,3K", StackFormatter.quantityToRSDecimalStack(100_300));
|
||||
assertEquals("100.3K", StackFormatter.quantityToRSDecimalStack(100_300));
|
||||
assertEquals("1M", StackFormatter.quantityToRSDecimalStack(1_000_000));
|
||||
assertEquals("8,4M", StackFormatter.quantityToRSDecimalStack(8_450_000));
|
||||
assertEquals("8.4M", StackFormatter.quantityToRSDecimalStack(8_450_000));
|
||||
assertEquals("10M", StackFormatter.quantityToRSDecimalStack(10_000_000));
|
||||
assertEquals("12,8M", StackFormatter.quantityToRSDecimalStack(12_800_000));
|
||||
assertEquals("12.8M", StackFormatter.quantityToRSDecimalStack(12_800_000));
|
||||
assertEquals("100M", StackFormatter.quantityToRSDecimalStack(100_000_000));
|
||||
assertEquals("250,1M", StackFormatter.quantityToRSDecimalStack(250_100_000));
|
||||
assertEquals("250.1M", StackFormatter.quantityToRSDecimalStack(250_100_000));
|
||||
assertEquals("1B", StackFormatter.quantityToRSDecimalStack(1_000_000_000));
|
||||
assertEquals("1,5B", StackFormatter.quantityToRSDecimalStack(1500_000_000));
|
||||
assertEquals("2,1B", StackFormatter.quantityToRSDecimalStack(Integer.MAX_VALUE));
|
||||
assertEquals("1.5B", StackFormatter.quantityToRSDecimalStack(1500_000_000));
|
||||
assertEquals("2.1B", StackFormatter.quantityToRSDecimalStack(Integer.MAX_VALUE));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user