Convert random number to integer in Spring Expression Language

Viewed 1567

I have using spring schedule job and would like randomly delay the job by input initialDelayString = "#{T(java.lang.Math).random() * 10000 }". I can generate the random number but the value is a long value (e.g. 4242.423422432)

How can I convert the value to integer using SpEL?

1 Answers

First of all, the current value is not a long, but rather a double as per return type of Math.random().

Also, you can always convert a Double to int by calling .intValue() on it, e.g.:

    Double someValue = 2.56;
    int nearlyTheSame = someValue.intValue();
    System.out.println(nearlyTheSame);

So your Spring expression might be:

  #{(T(java.lang.Math).random() * 10000).intValue()}
Related