Resource Formatting numbers - Exp, items etc.

Discussion in 'Tutorials & Resources' started by Qosmiof2, Jan 23, 2016.

  1. Code (Text):
    1. public static String format(long i) {
    2.             String formatted = "";
    3.             String end = "";
    4.             NumberFormat format = NumberFormat.getInstance();
    5.             formatted = format.format(i).toString();
    6.             if (i >= 1000) {
    7.                 end = "k";
    8.                 if (i >= 1000000) {
    9.                     end = "M";
    10.                     if (i >= 1000000000) {
    11.                         end = "B";
    12.                     }
    13.                 }
    14.                 formatted = formatted.split("\\.")[0] + "." + formatted.split("\\.")[1].substring(0, 2) + end;
    15.             }
    16.             return formatted;
    17.         }
    Gist:
    NewFormat.java · GitHub

    Examples:

    Code (Text):
    1.  
    2. 1.00k <--- 1000
    3. 11.92k <--- 11,929
    4. 158.39k <--- 158,390
    5. 999.99k <--- 999,999
    6. 1.00M <--- 1,000,001
    7. 12.34B <--- 12,345,678,912
    8.  
     
    #1 Qosmiof2, Jan 23, 2016
    Last edited: Oct 4, 2016
  2. Can you list some outputs? :D
     
    Qosmiof2 likes this.
  3. Nice, reminds me of something I made a little while ago for formatting numbers into strings :p

    Edit: Found it xD
    Code (Text):
    1.  
    2.     public static String toPhrase(long l, NumberFormat format){
    3.         String res = "";
    4.         double d = l;
    5.         while(d > 1000){
    6.             if (d >= Math.pow(10, 12)) {
    7.                 res = "T" + res;
    8.             } else if(d >= Math.pow(10, 9)){
    9.                 res = "B" + res;
    10.                 d /= Math.pow(10, 9);
    11.             } else if (d >= Math.pow(10, 6)){
    12.                 res = "M" + res;
    13.                 d /= Math.pow(10, 6);
    14.             } else if (d >= Math.pow(10, 3)){
    15.                 res = "K" + res;
    16.                 d /= Math.pow(10, 3);
    17.             }
    18.         }
    19.         if(format == null)
    20.             res = d + res;
    21.         else
    22.             res = format.format(d) + res;
    23.         return res;
    24.     }
    25.  
    26.     public static void main(String[] args) {
    27.         System.out.println(toPhrase(12345678912l, null)); //12.345678912B
    28.         System.out.println(toPhrase(12345678912l, DecimalFormat.getCurrencyInstance())); //$12.35B
    29.     }
    30.  
    toPhrase · GitHub

    I also have the code to reverse it :p
     
    Qosmiof2 likes this.
  4. Edited OP :)
    --- Double Post Merged, Jan 23, 2016, Original Post Date: Jan 23, 2016 ---
    Nice. Check skype bro
     
  5. Thank you sir.
     
    Qosmiof2 likes this.
  6. No problem :)
     

Share This Page

Loading...