# can someone please explain java code to me

I'm not a new bie by any means, but I found a rather perculier java code that execute differently from one line to another.

with double value = 0.349575

double val1 = value * 100;

double val2 = Math.round(val1);

double val3 = val2/100;

this produces 0.35 in val3.

double val3 = Math.round(value * 100)/100;

this produces 0.0 in val3.

can someone please explain what the difference is? as the second one should evaluate in the same order as the first is setup.

Cheers for explanation anyone.

chris

Math.round returns a long and [url=http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#239829]according to the spec[/url]: If this promoted type is int or long, then integer arithmetic is performed; if this promoted type is float or double, then floating-point arithmetic is performed.

So this work get the result you want:

[code]Math.round(value * 100)/100d[/code]

@Dmitriy: Math.round(value * 100) returns 35

Message was edited by: walterln