¿Java ScriptEngine falla por la adición 'larga'?

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?


preguntado el 29 de junio de 12 a las 19:06

1 Respuestas

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

Respondido el 29 de junio de 12 a las 19:06

FYI, Float is 32-bit and Double is 64-bit, floating point number. - user845279

Darn the Float! Works like magic with Double! Thank you so much! - aswin parthasarathy

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.