Skip to content

Instantly share code, notes, and snippets.

@yelinaung
Created May 14, 2014 03:10
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 yelinaung/1e38e6b85d405fcd565a to your computer and use it in GitHub Desktop.
Save yelinaung/1e38e6b85d405fcd565a to your computer and use it in GitHub Desktop.
Insert Comma for every three digits after dot
private String insertComma(String digits) {
String result = digits;
if (digits.length() <= 3) {
return digits;
} else if (digits.contains(".")) {
String[] x = digits.split("\\.");
for (int i = 0; i < (x[0].length() - 1) / 3; i++) {
int commaPos = (x[0].length() - 3) - (3 * i);
result = result.substring(0, commaPos) + "," + result.substring(commaPos);
}
return result;
} else {
for (int i = 0; i < (digits.length() - 1) / 3; i++) {
int commaPos = (digits.length() - 3) - (3 * i);
result = result.substring(0, commaPos) + "," + result.substring(commaPos);
}
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment