You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@velocity.apache.org by nb...@apache.org on 2004/04/16 22:12:42 UTC

cvs commit: jakarta-velocity-tools/src/java/org/apache/velocity/tools/generic MathTool.java

nbubna      2004/04/16 13:12:42

  Modified:    src/java/org/apache/velocity/tools/generic MathTool.java
  Log:
  add methods for integer division and modulus
  
  Revision  Changes    Path
  1.9       +51 -1     jakarta-velocity-tools/src/java/org/apache/velocity/tools/generic/MathTool.java
  
  Index: MathTool.java
  ===================================================================
  RCS file: /home/cvs/jakarta-velocity-tools/src/java/org/apache/velocity/tools/generic/MathTool.java,v
  retrieving revision 1.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- MathTool.java	12 Mar 2004 20:50:38 -0000	1.8
  +++ MathTool.java	16 Apr 2004 20:12:42 -0000	1.9
  @@ -146,6 +146,56 @@
   
   
       /**
  +     * Does integer division on the int values of the specified numbers.
  +     * 
  +     * <p>So, $math.idiv('5.1',3) will return '1', 
  +     *    and $math.idiv(6,'3.9') will return '2'.</p>
  +     *
  +     * @param num1 the first number
  +     * @param num2 the second number
  +     * @return the result of performing integer division
  +     *         on the operands.
  +     * @see #toInteger
  +     */
  +    public Integer idiv(Object num1, Object num2)
  +    {
  +        Number n1 = toNumber(num1);
  +        Number n2 = toNumber(num2);
  +        if (n1 == null || n2 == null || n2.intValue() == 0)
  +        {
  +            return null;
  +        }
  +        int value = n1.intValue() / n2.intValue();
  +        return new Integer(value);
  +    }
  +
  +
  +    /**
  +     * Does integer modulus on the int values of the specified numbers.
  +     *
  +     * <p>So, $math.mod('5.1',3) will return '2',
  +     *    and $math.mod(6,'3.9') will return '0'.</p>
  +     *
  +     * @param num1 the first number
  +     * @param num2 the second number
  +     * @return the result of performing integer modulus
  +     *         on the operands.
  +     * @see #toInteger
  +     */
  +    public Integer mod(Object num1, Object num2)
  +    {
  +        Number n1 = toNumber(num1);
  +        Number n2 = toNumber(num2);
  +        if (n1 == null || n2 == null || n2.intValue() == 0)
  +        {
  +            return null;
  +        }
  +        int value = n1.intValue() % n2.intValue();
  +        return new Integer(value);
  +    }
  +
  +
  +    /**
        * @param num1 the first number
        * @param num2 the second number
        * @return the largest of the numbers or 
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-dev-help@jakarta.apache.org