Take the 2-minute tour ×

I'm using the ScriptEngine to perform calculations specified in a String. I tried a simple program as below:

 public static void main(String[] args){
    ScriptEngineManager mgr = new ScriptEngineManager();
    ScriptEngine engine = mgr.getEngineByName("JavaScript");
    Float f;
    try {
        f = new Float(engine.eval("1340984972921+7200000").toString());
    } catch (Exception e){}

This gives the output of 1340992126976 but the correct answer is 1340992172921

This seems to work for reasonably large integers but fails for long values. I want them as long values because they are timestamps and I want to use them as such. Any ideas on what could make this work?


share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

Try changing the data type from Float to Double. I think it's a precision problem.

share|improve this answer
FYI, Float is 32-bit and Double is 64-bit, floating point number. –  user845279 Jun 29 '12 at 18:44
Darn the Float! Works like magic with Double! Thank you so much! –  Aswin Parthasarathy Jun 29 '12 at 18:45
add comment

Your Answer


