How to convert number into K thousands M million and B billion suffix in jsp

前端 未结 2 2069
时光取名叫无心
时光取名叫无心 2020-12-13 03:01

How can i convert number into K thousands M million and B billion suffix in jsp

e.g

1111 as 1.111 K etc

2条回答
  •  自闭症患者
    2020-12-13 03:32

    //To remove zero from 1.0k

    public static String coolNumberFormat(long count) {
            if (count < 1000) return "" + count;
            int exp = (int) (Math.log(count) / Math.log(1000));
            DecimalFormat format = new DecimalFormat("0.#");
            String value = format.format(count / Math.pow(1000, exp));
            return String.format("%s%c", value, "kMBTPE".charAt(exp - 1));
        }
    

提交回复
热议问题