0

i'm traying to put in velocity context one method:

ctx.put("round", roundServiceTime(serviceTimeRound));


public int roundServiceTime (int serviceTimeRound) {
    double sum = serviceTimeRound/60;
    this.serviceTimeRound = (int)Math.ceil((double)sum);
    return serviceTimeRound;

}

error line: #set( $val = $round(90))

and getting error :

Encountered "(" at line 175, column 20.

Was expecting one of: ... ... "-" ... "+" ... "*" ... "/" ... "%" ... "&&" ... "||" ... "<" ... "<=" ... ">" ... ">=" ... "==" ... "!=" ... ...

where is the problem ?

PDS
  • 562
  • 3
  • 13
  • 27

1 Answers1

2

after looking at your code once more i think you want to put some kind of "magic link" into your context so you can simply call that method. But the context only contains objects.

You can achieve what you want by putting your method into a utility class:

public class Rounder {

    public static final Rounder INSTANCE = new Rounder(); 

    public int roundServiceTime (int serviceTimeRound) {
        double sum = serviceTimeRound / 60.0;
        return (int)Math.ceil(sum);
    }
}

then you can put an instance of your utility class into your context:

ctx.put("rounder", Rounder.INSTANCE);

and use it in your template:

$rounder.roundServiceTime($someValue)
Marco Forberg
  • 2,634
  • 5
  • 22
  • 33