Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

Sign up now!

Resource Formatting numbers - Exp, items etc.

I've been called a god before.
Joined
Aug 5, 2014
Messages
3,212
Code:
public static String format(long i) {
            String formatted = "";
            String end = "";
            NumberFormat format = NumberFormat.getInstance();
            formatted = format.format(i).toString();
            if (i >= 1000) {
                end = "k";
                if (i >= 1000000) {
                    end = "M";
                    if (i >= 1000000000) {
                        end = "B";
                    }
                }
                formatted = formatted.split("\\.")[0] + "." + formatted.split("\\.")[1].substring(0, 2) + end;
            }
            return formatted;
        }

Gist:
NewFormat.java · GitHub

Examples:

Code:
1.00k <--- 1000
11.92k <--- 11,929
158.39k <--- 158,390
999.99k <--- 999,999
1.00M <--- 1,000,001
12.34B <--- 12,345,678,912
 
Last edited:
Joined
Dec 10, 2014
Messages
3,255
Nice, reminds me of something I made a little while ago for formatting numbers into strings :p

Edit: Found it xD
Code:
    public static String toPhrase(long l, NumberFormat format){
        String res = "";
        double d = l;
        while(d > 1000){
            if (d >= Math.pow(10, 12)) {
                res = "T" + res;
            } else if(d >= Math.pow(10, 9)){
                res = "B" + res;
                d /= Math.pow(10, 9);
            } else if (d >= Math.pow(10, 6)){
                res = "M" + res;
                d /= Math.pow(10, 6);
            } else if (d >= Math.pow(10, 3)){
                res = "K" + res;
                d /= Math.pow(10, 3);
            }
        }
        if(format == null)
            res = d + res;
        else
            res = format.format(d) + res;
        return res;
    }

    public static void main(String[] args) {
        System.out.println(toPhrase(12345678912l, null)); //12.345678912B
        System.out.println(toPhrase(12345678912l, DecimalFormat.getCurrencyInstance())); //$12.35B
    }
toPhrase · GitHub

I also have the code to reverse it :p
 
I've been called a god before.
Joined
Aug 5, 2014
Messages
3,212
Can you list some outputs? :D
Edited OP :)
 
Nice, reminds me of something I made a little while ago for formatting numbers into strings :p

Edit: Found it xD
Code:
    public static String toPhrase(long l, NumberFormat format){
        String res = "";
        double d = l;
        while(d > 1000){
            if (d >= Math.pow(10, 12)) {
                res = "T" + res;
            } else if(d >= Math.pow(10, 9)){
                res = "B" + res;
                d /= Math.pow(10, 9);
            } else if (d >= Math.pow(10, 6)){
                res = "M" + res;
                d /= Math.pow(10, 6);
            } else if (d >= Math.pow(10, 3)){
                res = "K" + res;
                d /= Math.pow(10, 3);
            }
        }
        if(format == null)
            res = d + res;
        else
            res = format.format(d) + res;
        return res;
    }

    public static void main(String[] args) {
        System.out.println(toPhrase(12345678912l, null)); //12.345678912B
        System.out.println(toPhrase(12345678912l, DecimalFormat.getCurrencyInstance())); //$12.35B
    }
toPhrase · GitHub

I also have the code to reverse it :p
Nice. Check skype bro
 
Top