private double arith(int rowIndex){

        if(params.length != 3){
            throw new IllegalArgumentException("Wrong number of Parameters for arithmetic expression");
        }

        double res = 0;
        try{
            final RecordList.Record currentRecord = recordList.getRecords().get(rowIndex);
            String operator = params[2];

            double firstValue = Double.parseDouble(currentRecord.getValueByFieldName(params[0]));

            double secondValue = Double.parseDouble(currentRecord.getValueByFieldName(params[1]));


            if("+".equals(operator)){
                res = firstValue + secondValue;
            }else if("-".equals(operator)){
                res = firstValue - secondValue;
            }else if("*".equals(operator)){
                res = firstValue * secondValue;
            }else if("/".equals(operator)){
                res = firstValue / secondValue;
            }else if("%".equals(operator)){
                res = (firstValue / secondValue) * 100;
            }

            NumberFormat decimalFormat = NumberFormat.getFormat(".##");
            res = Double.parseDouble(decimalFormat.format(res));
        }catch(Exception e){
            System.err.println("Error calculating table arithmetic function");
        }
        return res;

    }
private double arith ( int rowIndex ) { if ( params . length != 3 ) { throw new IllegalArgumentException ( "Wrong number of Parameters for arithmetic expression" ) ; } double res = 0 ; try { final RecordList . Record currentRecord = recordList . getRecords ( ) . get ( rowIndex ) ; String operator = params [ 2 ] ; double firstValue = Double . parseDouble ( currentRecord . getValueByFieldName ( params [ 0 ] ) ) ; double secondValue = Double . parseDouble ( currentRecord . getValueByFieldName ( params [ 1 ] ) ) ; if ( "+" . equals ( operator ) ) { res = firstValue + secondValue ; } else if ( "-" . equals ( operator ) ) { res = firstValue - secondValue ; } else if ( "*" . equals ( operator ) ) { res = firstValue * secondValue ; } else if ( "/" . equals ( operator ) ) { res = firstValue / secondValue ; } else if ( "%" . equals ( operator ) ) { res = ( firstValue / secondValue ) * 100 ; } NumberFormat decimalFormat = NumberFormat . getFormat ( ".##" ) ; res = Double . parseDouble ( decimalFormat . format ( res ) ) ; } catch ( Exception e ) { System . err . println ( "Error calculating table arithmetic function" ) ; } return res ; }
Calculate an arithmetic expression
In the config file use:
fieldName="func(ARITH(field1,field2,operator))"