Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@Qosmiof
Created January 23, 2016 16:01
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 Qosmiof/541c70451f737c033a7e to your computer and use it in GitHub Desktop.
Save Qosmiof/541c70451f737c033a7e to your computer and use it in GitHub Desktop.
System.out.println(format(11929) + " <--- 11,929");
System.out.println(format(158390) + " <--- 158,390");
System.out.println(format(999999) + " <--- 999,999");
System.out.println(format(1000) + " <--- 1000");
System.out.println(format(1000001) + " <--- 1,000,001");
System.out.println(format(12345678912l) + " <--- 12,345,678,912");
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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment