Converting values to unit prefixes in JSP page

后端 未结 3 1073
迷失自我
迷失自我 2021-01-14 06:39

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

3条回答
  •  醉酒成梦
    2021-01-14 07:15

    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");
    }
    

提交回复
热议问题