Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert number into K thousands M million and B billion suffix in jsp

Tags:

How can i convert number into K thousands M million and B billion suffix in jsp

e.g

1111 as 1.111 K etc

like image 511
jan5 Avatar asked Mar 19 '12 11:03

jan5


People also ask

How do you convert to thousands?

So, to convert from millions to thousands, multiply the value in millions by 1000; the result is a value in thousands. Similarly, to convert from a value in thousands to millions, multiple the value in thousands by 0.001, and you end up with a value in millions.

How do you convert numbers into millions?

So, to convert any number from crores to million, you need to multiply the number by 10 and it will become millions. If your number is 5 crores, you multiply 5 by 10 to get 50 million. The same formula can be altered to convert a number from million to crores.

How do you convert numbers into billions?

Million to billion conversion The terms million, billion, and trillion are used in the western numbering system. 1 billion is equal to 1000 million. Divide the number in million by 1000 to get the number in billion.


2 Answers

Adapting the answer from over here it 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 
like image 173
aioobe Avatar answered Sep 22 '22 11:09

aioobe


//To remove zero from 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));     } 
like image 23
Thomc Avatar answered Sep 25 '22 11:09

Thomc