| Method from org.apache.velocity.tools.generic.NumberTool Detail: |
public String format(Object obj) {
return format(getFormat(), obj);
}
Converts the specified object to a number and formats it according to
the pattern or style returned by #getFormat() . |
public String format(String format,
Object obj) {
return format(format, obj, getLocale());
}
Converts the specified object to a number and returns
a formatted string representing that number in the locale
returned by #getLocale() . |
public String format(String format,
Object obj,
Locale locale) {
Number number = toNumber(obj);
NumberFormat nf = getNumberFormat(format, locale);
if (number == null || nf == null)
{
return null;
}
return nf.format(number);
}
Converts the specified object to a number and returns
a formatted string representing that number in the specified
Locale . |
public String getFormat() {
return DEFAULT_FORMAT;
}
Return the pattern or style to be used for formatting numbers when none
is specified. This implementation gives a 'default' number format.
Subclasses may override this to provide a different default format.
NOTE: At some point in the future it may be feasible to configure
this value via the toolbox definition, but at present, it is not possible
to specify custom tool configurations there. For now you should just
override this in a subclass to have a different default. |
public Locale getLocale() {
return Locale.getDefault();
}
This implementation returns the default locale. Subclasses
may override this to return alternate locales. Please note that
doing so will affect all formatting methods where no locale is
specified in the parameters. |
public NumberFormat getNumberFormat(String format,
Locale locale) {
if (format == null)
{
return null;
}
NumberFormat nf = null;
int style = getStyleAsInt(format);
if (style < 0)
{
// we have a custom format
nf = new DecimalFormat(format, new DecimalFormatSymbols(locale));
}
else
{
// we have a standard format
nf = getNumberFormat(style, locale);
}
return nf;
}
Returns a NumberFormat instance for the specified
format and Locale . If the format specified is a standard
style pattern, then a number instance
will be returned with the number style set to the
specified style. If it is a custom format, then a customized
NumberFormat will be returned. |
protected NumberFormat getNumberFormat(int numberStyle,
Locale locale) {
try
{
NumberFormat nf;
switch (numberStyle)
{
case STYLE_NUMBER:
nf = NumberFormat.getNumberInstance(locale);
break;
case STYLE_CURRENCY:
nf = NumberFormat.getCurrencyInstance(locale);
break;
case STYLE_PERCENT:
nf = NumberFormat.getPercentInstance(locale);
break;
case STYLE_INTEGER:
nf = getIntegerInstance(locale);
break;
default:
// invalid style was specified, return null
nf = null;
}
return nf;
}
catch (Exception suppressed)
{
// let it go...
return null;
}
}
|
protected int getStyleAsInt(String style) {
// avoid needlessly running through all the string comparisons
if (style == null || style.length() < 6 || style.length() > 8) {
return -1;
}
if (style.equalsIgnoreCase("default"))
{
//NOTE: java.text.NumberFormat returns "number" instances
// as the default (at least in Java 1.3 and 1.4).
return STYLE_NUMBER;
}
if (style.equalsIgnoreCase("number"))
{
return STYLE_NUMBER;
}
if (style.equalsIgnoreCase("currency"))
{
return STYLE_CURRENCY;
}
if (style.equalsIgnoreCase("percent"))
{
return STYLE_PERCENT;
}
if (style.equalsIgnoreCase("integer"))
{
return STYLE_INTEGER;
}
// ok, it's not any of the standard patterns
return -1;
}
Checks a string to see if it matches one of the standard
NumberFormat style patterns:
NUMBER, CURRENCY, PERCENT, INTEGER, or DEFAULT.
if it does it will return the integer constant for that pattern.
if not, it will return -1. |
public Number toNumber(Object obj) {
return toNumber(getFormat(), obj, getLocale());
}
|
public Number toNumber(String format,
Object obj) {
return toNumber(format, obj, getLocale());
}
Converts an object to an instance of Number using the
specified format and the Locale returned by
#getLocale() if the object is not already an instance
of Number. |
public Number toNumber(String format,
Object obj,
Locale locale) {
if (obj == null)
{
return null;
}
if (obj instanceof Number)
{
return (Number)obj;
}
try
{
NumberFormat parser = getNumberFormat(format, locale);
return parser.parse(String.valueOf(obj));
}
catch (Exception e)
{
return null;
}
}
Converts an object to an instance of Number using the
specified format and Locale if the object is not already
an instance of Number. |