【问题标题】:How to convert number into K thousands M million and B billion suffix in jsp如何在jsp中将数字转换为K千M百万和B十亿后缀
【发布时间】:2019-11-28 03:03:30
【问题描述】:

如何在jsp中将数字转换为K千百万和B十亿后缀

例如

1111 作为1.111 K

【问题讨论】:

标签: java jsp


【解决方案1】:

here 调整答案,它应该看起来像

public static String withSuffix(long count) {
    if (count < 1000) return "" + count;
    int exp = (int) (Math.log(count) / Math.log(1000));
    return String.format("%.1f %c",
                         count / Math.pow(1000, exp),
                         "kMGTPE".charAt(exp-1));
}

测试代码:

for (long num : new long[] { 0, 27, 999, 1000, 110592,
                             28991029248L, 9223372036854775807L })
   System.out.printf("%20d: %8s%n", num, withSuffix(num));

输出:

                   0:        0
                  27:       27
                 999:      999
                1000:    1.0 k
              110592:  110.6 k
         28991029248:   29.0 G
 9223372036854775807:    9.2 E

【讨论】:

  • 写的真漂亮!谢谢。
  • 如果十进制为0,如何将1.0k变为1k?
  • 这个答案很漂亮。
  • 简单而精彩的答案。
  • @BeeingJk 这里 public static String coolNumberFormat(long count) { if (count
【解决方案2】:

//从 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));
    }

【讨论】:

    猜你喜欢
    • 2021-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-05
    • 2013-05-12
    • 2021-12-14
    • 1970-01-01
    相关资源
    最近更新 更多