Created
May 14, 2014 03:10
-
-
Save yelinaung/1e38e6b85d405fcd565a to your computer and use it in GitHub Desktop.
Insert Comma for every three digits after dot
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
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