- 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
Asked
Active
Viewed 437 times
0

Catarina Ferreira
- 1,824
- 5
- 17
- 26

Priya
- 37
- 2
-
3Check this out. http://stackoverflow.com/a/30661479/2975371 – Danieboy Mar 17 '17 at 10:14
-
1Welcome to stackoverflow! Please visit https://stackoverflow.com/help/how-to-ask – Olaia Mar 17 '17 at 10:27
-
http://stackoverflow.com/questions/9769554/how-to-convert-number-into-k-thousands-m-million-and-b-billion-suffix-in-jsp – Aditya Vyas-Lakhan Mar 17 '17 at 12:25
1 Answers
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