package hall;

import java.math.*;

public class BigIntegerFormatter {
  public static String format(String bigIntString) {
    int origLength = bigIntString.length();
    int numCommas = (origLength - 1) / 3;
    int formattedLength = origLength + numCommas;
    StringBuffer buff = new StringBuffer(formattedLength);
    buff.setLength(formattedLength);
    int index1 = origLength-1;
    int index2 = formattedLength-1;
    while(index1 >= 0) {
      buff.setCharAt(index2--, bigIntString.charAt(index1--));
      if (index1 < 0)
        break;
      buff.setCharAt(index2--, bigIntString.charAt(index1--));
      if (index1 < 0)
        break;
      buff.setCharAt(index2--, bigIntString.charAt(index1--));
      if (index1 < 0)
        break;
      if (index1 >= 0)
        buff.setCharAt(index2--, ',');
    }
    return(buff.toString());
  }

  public static String format(Object bigInt) {
    return(format(bigInt.toString()));
  }
}
