Skip to content

Instantly share code, notes, and snippets.

@slashnhax
Created January 23, 2016 15:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save slashnhax/c986acebcf7d25e3bff5 to your computer and use it in GitHub Desktop.
Save slashnhax/c986acebcf7d25e3bff5 to your computer and use it in GitHub Desktop.
toPhrase
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));
System.out.println(toPhrase(12345678912l, DecimalFormat.getCurrencyInstance()));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment