It was a pretty standard scenario: Lindsay F took over a small project from another department, and that project had a lot of “fun” surprises. Despite accomplishing a fairly simple task – reading in a fixed-width datafile, cleaning up a few things, and inserting it into a database – the application managed to be incredibly complex. Personaly, I blame the language they used: Java.

You’d think that the developers of Java would provide a simple way to convert a string, say "000000028000", into a number, say 280.00. At least Java does provide the System.out.println method. As the comments show, that proved to be an invauable tool in solving this almost impossible string-to-number conversion task ...

if (amount != null && amount.trim().length() > 0) {
  for (int i = 0; i < amount.length(); i++) {
    char c = amount.charAt(i);
    // System.out.println(i + "  " + String.valueOf(c));
    if (!String.valueOf(c).equals("0")) {
    //   System.out.println(i);
      if (amount.substring(i).endsWith("00")) {
        amount = amount.substring(i,amount.length()-2) + ".00";
      } else {
        //System.out.println("old amount" + amount);
        if (i+2 < amount.length()) {
          amount = amount.substring(i,amount.length()-2) + "." + 
              amount.substring(amount.length()-2,amount.length());
        } else {
          if (i+2 == amount.length()) {
            amount = "0." + amount.substring(i);
          } else {
            amount = "0.0" + amount.substring(i);
          }
        }
        //System.out.println("new amount" + amount);
      }
      break;
    }
  }
}
[Advertisement] BuildMaster allows you to create a self-service release management platform that allows different teams to manage their applications. Explore how!