How to convert a number into a formatted string?
The format method automatically does the commas and periods or visa versa (depending on whether your profile is in the US or elsewhere)
Assuming the $ sign applies to all cases in your org you can simply use
Decimal input = 2000;
String output = '$' + String.valueOf(input.format());
Note the String.valueOf(). This is required because the input variable is defined as a Decimal.
Edit: I noticed a bug with the Decimal.format() method where it will not show any numbers past the decimal point if there are only zeros there, ex. $100.00. To solve this I came up with this method.
private String getCents(Decimal x){
String y = String.valueOf(x);
String z = '.';
if(y.contains(',')) z = ',';
y = y.substring(0, y.indexOf(z));
if(x - Decimal.valueOf(y) == 0)
return String.valueOf(x.format()) + z + '00';
else return String.valueOf(x.format());
}
Then to update the example above, it would be:
Decimal input = 2000;
String output = '$' + getCents(input);
This should work, if its until 2 decimals:
Decimal dec;
String amount;
if (!string.valueof(dec.format()).right(3).contains('.')){
amount = '$' + string.valueof(dec.format()) + '.00';
}else if (string.valueof(dec.format()).right(2).contains('.')){
amount = '$' + string.valueof(dec.format()) + '0';
}else {
amount = '$' + string.valueof(dec.format());
}