您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

如何在JSP中将数字转换为K千M亿和B十亿后缀

如何在JSP中将数字转换为K千M亿和B十亿后缀

这里改编答案,看起来应该像

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
其他 2022/1/1 18:14:59 有486人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶