How can I convert values to to other units in JSP page. For example if I get value 1001 and I want to only display 1K, or when I get 1 000 001 I would like to display 1M and
static String toSymbol(int in) {
String[] p = { "", "K", "M", "G" };
int k = 1000;
assert pow(k, p.length) - 1 > Integer.MAX_VALUE;
int x = in;
for (int i = 0; i < p.length; i++) {
if (x < 0 ? -k < x : x < k) return x + p[i];
x = x / k;
}
throw new RuntimeException("should not get here");
}