Skip to content

Instantly share code, notes, and snippets.

@jessejohnson
Created November 8, 2016 11:34
Show Gist options
  • Save jessejohnson/98ffc6bf8ea8f1073975ed6e7c57ab7c to your computer and use it in GitHub Desktop.
Save jessejohnson/98ffc6bf8ea8f1073975ed6e7c57ab7c to your computer and use it in GitHub Desktop.
format large numbers like on Twitter, etc.
private String formatLargeInteger(int largeInt){
if(largeInt < 10000){
//ie. 0 - 9,999
return String.valueOf(largeInt);
}else if(largeInt >= 10000 && largeInt < 1000000){
//ie. 10,000 - 999,999
String s = String.valueOf(largeInt);
return s.substring(0, s.length() - 3) + "K"; //10K - 999K
}else {
//ie. 1,000,000 - inf
String s = String.valueOf(largeInt);
return s.substring(0, s.length() - 6) + "M"; //1M - infM
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment