You may try this code by making this method call
public class Utilities{
private static int startingPivot;
private static final int INDENT = 3;
public static StringBuffer decimalize(int input){
String conv = Integer.toString(input);
StringBuffer sb = new StringBuffer();
int commaIndex = conv.length() / INDENT ;
int strLength = conv.length();
startingPivot = (strLength) - (commaIndex * INDENT );
if (startingPivot == 0) {
startingPivot = INDENT ;
}
for (int index = 0; index < strLength; index++) {
if (index == startingPivot) {
sb.append(",");
startingPivot = startingPivot + INDENT;
}
sb.append(conv.charAt(index));
}
return sb;
}
}
and the output is 1,234,567,890
System.out.println(Utilities.decimalize(1234567890));
NEAT :D
0 comments:
Post a Comment