Thanks everyone. I have finally settled on this method. I am posting it
in case another newbie (Geez I hate that name) has a similar question.
A few quick notes.
public String SpaceNumbersForDisplay(String s, char mSeparator){
String formattedString="";
String mIntegerPart="";
String mDecimalPart="";
//If the number is Zero then just return 0.00
if(s.equals("0")){
return "0.00";
1. Unnecessary. (See below.)
2. You're trying to normalize, but you really don't. (Pass in "0.0",
get a different result than for "0". Unexpected for clients.)
3. Not localized.
}//end if
//Check if there are decimals
if(s.indexOf(".")==-1){
1. Unnecessary (DecimalFormat handles decimal fractions just fine).
2. Breaks if default locale doesn't use '.' as decimal separator.
mIntegerPart=s;
mDecimalPart=".00";
}else{
mIntegerPart= s.substring(0,((int)s.indexOf(".")));
mDecimalPart= s.substring(((int)s.indexOf(".")),(int)s.length());
}//end if
//Get the integer form of the String
int num = Integer.parseInt(mIntegerPart);
//Create new DecimalFormat object
DecimalFormat dfNum = new DecimalFormat();
//Create new DecimalFormatSynbols object
Don't document what you're doing unless it's non-obvious. Document
why you're doing it is much more important (again, unless it's
obvious). If anything, say why you need the DecimalFormatSymbols.
DecimalFormatSymbols dfsNum= dfNum.getDecimalFormatSymbols();
//Apply the symbol
dfsNum.setGroupingSeparator(mSeparator);
dfNum.setDecimalFormatSymbols(dfsNum);
//Apply the thousands separator method of DecimalFormat
formattedString = dfNum.format(num) + mDecimalPart;
//return the formatted string
return formattedString;
} //end SpaceNumbersForDisplay
In general, it's usually best to see if the standard library can do
what you're trying to achieve before starting to implement lots of
special cases yourself. Chances are you'll overlook some finer points
that are automatically taken care of in the library. This is a good
example. There is no need to do all those decimal point gymnastics,
and it won't work as you intended if the platform locale doesn't use
a dot as decimal separator (a comma is customary in many parts of the
world).
Look again at DecimalFormat, it can do all you want to do and more
in just a handful of lines.
Cheers, Tilman