public String format(Object dataToFormat) throws JspException
{
if (dataToFormat == null) {
return null;
}
InternalStringBuilder formattedString = new InternalStringBuilder(32);
DecimalFormat numberFormat = null;
// get the number format. The type has been validated when it was set on the tag.
if (locale == null) {
if ((type == null) || (type.equals("number"))) {
numberFormat = (DecimalFormat) java.text.NumberFormat.getNumberInstance();
}
else if (type.equals("currency")) {
numberFormat = (DecimalFormat) java.text.NumberFormat.getCurrencyInstance();
}
else if (type.equals("percent")) {
numberFormat = (DecimalFormat) java.text.NumberFormat.getPercentInstance();
}
else {
assert(false) : "Invalid type was found:" + type;
}
}
else {
if ((type == null) || (type.equals("number"))) {
numberFormat = (DecimalFormat) java.text.NumberFormat.getNumberInstance(locale);
}
else if (type.equals("currency")) {
numberFormat = (DecimalFormat) java.text.NumberFormat.getCurrencyInstance(locale);
}
else if (type.equals("percent")) {
numberFormat = (DecimalFormat) java.text.NumberFormat.getPercentInstance(locale);
}
else {
assert(false) : "Invalid type was found:" + type;
}
}
// format the number, apply the pattern specified
try {
if (getPattern() != null)
numberFormat.applyPattern(getPattern());
}
catch (Exception e) {
throw new JspException(Bundle.getString("Tags_NumberFormatPatternException", e.getMessage()), e);
}
// parse the number
if (dataToFormat.toString().length() == 0) {
return "";
}
try {
double number = Double.parseDouble(dataToFormat.toString());
formattedString.append(numberFormat.format(number));
}
catch (Exception e) {
throw new JspException(Bundle.getString("Tags_NumberFormatParseException", e.getMessage()), e);
}
return formattedString.toString();
}