Java jsp中如何将数字转换为K千M百万和B亿后缀
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9769554/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 07:26:52 来源:igfitidea点击:
How to convert number into K thousands M million and B billion suffix in jsp
提问by jan5
How can i convert number into K thousands M million and B billion suffix in jsp
我如何在jsp中将数字转换为K千M百万和B亿后缀
e.g
例如
1111
as 1.111 K
etc
1111
如1.111 K
等
采纳答案by aioobe
Adapting the answer from over hereit should look something like
调整这里的答案它应该看起来像
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));
}
Test code:
测试代码:
for (long num : new long[] { 0, 27, 999, 1000, 110592,
28991029248L, 9223372036854775807L })
System.out.printf("%20d: %8s%n", num, withSuffix(num));
Output:
输出:
0: 0
27: 27
999: 999
1000: 1.0 k
110592: 110.6 k
28991029248: 29.0 G
9223372036854775807: 9.2 E
回答by Thomc
//To remove zero from 1.0k
//从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));
}