/*
 * =============================================================================
 * 
 *   Copyright (c) 2011-2012, The THYMELEAF team (http://www.thymeleaf.org)
 * 
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 * 
 * =============================================================================
 */
package org.thymeleaf.standard.expression;

import java.math.BigDecimal;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.thymeleaf.Configuration;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.IProcessingContext;
import org.thymeleaf.exceptions.TemplateProcessingException;
import org.thymeleaf.util.ObjectUtils;



/**
 * 
 * @author Daniel Fern&aacute;ndez
 * 
 * @since 1.1
 *
 */
public final class DivisionExpression extends MultiplicationDivisionRemainderExpression {


    private static final long serialVersionUID = -6480768503994179971L;



    private static final Logger logger = LoggerFactory.getLogger(DivisionExpression.class);




    
    public DivisionExpression(final Expression left, final Expression right) {
        super(left, right);
    }
    

    
    @Override
    public String getStringRepresentation() {
        return getStringRepresentation(DIVISION_OPERATOR);
    }
    
    
    
    
    static Object executeDivision(final Configuration configuration, final IProcessingContext processingContext, 
            final DivisionExpression expression, final IStandardVariableExpressionEvaluator expressionEvaluator,
            final StandardExpressionExecutionContext expContext) {

        if (logger.isTraceEnabled()) {
            logger.trace("[THYMELEAF][{}] Evaluating division expression: \"{}\"", TemplateEngine.threadIndex(), expression.getStringRepresentation());
        }
        
        Object leftValue = 
            Expression.execute(configuration, processingContext, expression.getLeft(), expressionEvaluator, expContext);

        Object rightValue = 
            Expression.execute(configuration, processingContext, expression.getRight(), expressionEvaluator, expContext);
        
        if (leftValue == null) {
            leftValue = "null";
        }
        if (rightValue == null) {
            rightValue = "null";
        }
        
        final BigDecimal leftNumberValue = ObjectUtils.evaluateAsNumber(leftValue);
        final BigDecimal rightNumberValue = ObjectUtils.evaluateAsNumber(rightValue);
        if (leftNumberValue != null && rightNumberValue != null) {
            // Addition will act as a mathematical 'plus'
            return leftNumberValue.divide(rightNumberValue);
        }
        
        throw new TemplateProcessingException(
            "Cannot execute division: operands are \"" + LiteralValue.unwrap(leftValue) + "\" and \"" + LiteralValue.unwrap(rightValue) + "\"");
        
    }

    
}

@@ -897,16 +897,47 @@
 Decimal;
+%0Aimport java.math.RoundingMode;
 %0A%0Aimport
@@ -3046,114 +3046,557 @@
     
-// Addition will ac
+try %7B%0A                return leftNumberValue.divide(rightNumberValue);%0A            %7D catch (final ArithmeticException ignored) %7B%0A                // Resul
 t 
+h
 as a 
-mathematical 'plus'%0A            return leftNumberValue.divide(rightNumberValue);
+non-terminating decimal expansion (like 100/3), so%0A                // we just use a minimum arbitrary scale (10) and HALF_UP rounding mode%0A                return leftNumberValue.divide(%0A                        rightNumberValue, %0A                        Math.max(Math.max(leftNumberValue.scale(),rightNumberValue.scale()), 10), %0A                        RoundingMode.HALF_UP);%0A            %7D
 %0A   

Fix ArithmeticException in numeric literal divisions with non-terminating decimal expansion.