Class NumberTool


  • @DefaultKey("number")
    public class NumberTool
    extends FormatConfig
    Tool for working with Number in Velocity templates. It is useful for accessing and formatting arbitrary Number objects. Also the tool can be used to retrieve NumberFormat instances or make conversions to and from various number types.

     Example uses:
      $myNumber                            -> 13.55
      $number.format($myNumber)   -> 13.6
      $number.currency($myNumber) -> $13.55
      $number.integer($myNumber)  -> 13
    
     Example tools.xml config (if you want to use this with VelocityView):
     <tools>
       <toolbox scope="application">
         <tool class="org.apache.velocity.tools.generic.MathTool"/>
       </toolbox>
     </tools>
     

    This tool is entirely threadsafe, and has no instance members. It may be used in any scope (request, session, or application). As such, the methods are highly interconnected, and overriding key methods provides an easy way to create subclasses that use a non-default format or locale.

    Since:
    VelocityTools 1.2
    Version:
    $Id$
    Author:
    Nathan Bubna, Mike Kienenberger
    • Constructor Detail

      • NumberTool

        public NumberTool()
    • Method Detail

      • currency

        public String currency​(Object obj)
        Convenience method equivalent to $number.format("currency", $foo).
        Since:
        VelocityTools 1.3
      • integer

        public String integer​(Object obj)
        Convenience method equivalent to $number.format("integer", $foo).
        Since:
        VelocityTools 1.3
      • number

        public String number​(Object obj)
        Convenience method equivalent to $number.format("number", $foo).
        Since:
        VelocityTools 1.3
      • percent

        public String percent​(Object obj)
        Convenience method equivalent to $number.format("percent", $foo).
        Since:
        VelocityTools 1.3
      • format

        public String format​(String format,
                             Object obj)
        Converts the specified object to a number and returns a formatted string representing that number in the locale returned by LocaleConfig.getLocale().
        Parameters:
        format - the formatting instructions
        obj - the number object to be formatted
        Returns:
        a formatted string for this locale representing the specified number or null if the parameters are invalid
        See Also:
        format(String format, Object obj, Locale locale)
      • format

        public String format​(String format,
                             Object obj,
                             Locale locale)
        Converts the specified object to a number and returns a formatted string representing that number in the specified Locale.
        Parameters:
        format - the custom or standard pattern to be used
        obj - the number object to be formatted
        locale - the Locale to be used when formatting
        Returns:
        a formatted string representing the specified number or null if the parameters are invalid
      • getNumberFormat

        public NumberFormat getNumberFormat​(String format,
                                            Locale locale)
        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.
        Parameters:
        format - the custom or standard formatting pattern to be used
        locale - the Locale to be used
        Returns:
        an instance of NumberFormat
        See Also:
        NumberFormat
      • getNumberFormat

        @Deprecated
        protected NumberFormat getNumberFormat​(int numberStyle,
                                               Locale locale)
        Deprecated.
        Returns a NumberFormat instance for the specified number style and Locale.
        Parameters:
        numberStyle - the number style (number will be ignored if this is less than zero or the number style is not recognized)
        locale - the Locale to be used
        Returns:
        an instance of NumberFormat or null if an instance cannot be constructed with the given parameters
      • getStyleAsInt

        @Deprecated
        protected int getStyleAsInt​(String style)
        Deprecated.
        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.
        Parameters:
        style - the string to be checked
        Returns:
        the int identifying the style pattern
        See Also:
        NumberFormat