2017-03-17 40 views
0
  • 我有一个应用程序的报价有喜欢和分享 函数。
  • 我喜欢展示和分享数它与正常的方式像 100,1050等,现在我想表明喜欢1.1K号.... 1.2M等
  • 我已经搜索了很多,但没有找到任何适当的方式来做到这一点。 让我知道如果有人在这里可以给我任何参考或想法 相同。
  • 我不想要更改号码,如果它是三位数字。 我想转换它 如果有数量是1000-1049比它将1K和如果数字是 1050-1100比它会显示1.1K等等。从大到小的Java Android

    让我知道你是否有任何想法做同样的事情。谢谢

+3

看看这个。 http://stackoverflow.com/a/30661479/2975371 – Danieboy

+1

欢迎来到stackoverflow!请访问https://stackoverflow.com/help/how-to-ask – Olaia

+0

http://stackoverflow.com/questions/9769554/how-to-convert-number-into-k-thousands-m-million-and- b-数十亿后缀中,JSP –

回答

0

不久前有同样的任务。找到了一些不错的代码片段,但没有链接了。

public static void main (String[]args) { 
    for (long num : new long[] { 999,1000,1049,1050,1100,5432, 19999, 654321, 7000000, 7654321, 80010000, 88888888, 9999999 }) 
     System.out.println(formatNumber(num)); 
} 

public static String formatNumber(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)); 
}