0
  • I have one application of quotes which have likes and sharing function.
  • I am displaying likes and share number in it with normal way like 100,1050 etc. Now I want show number like 1.1K....1.2M etc.
  • I have searched lot for it but not finding any proper way to do it. Let me know if someone here can give me any reference or idea for same.
  • I do not want change number if it's in three digit. I want convert it if there number is 1000-1049 than it will be 1K and if number is like 1050-1100 than it will show 1.1K and so on.

    Let me know if You have any idea for do same. Thanks

Catarina Ferreira
  • 1,824
  • 5
  • 17
  • 26
Priya
  • 37
  • 2

1 Answers1

1

Had the same task a while ago. Found some nice code snippet, but don´t have the link anymore.

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));
}
Eritrean
  • 15,851
  • 3
  • 22
  • 28