-
-
Save Qosmiof/541c70451f737c033a7e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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