java - What is this method returning? -
what "(int)" in following return statement?
public double calc(double amount){ return (int)(amount * 100)/100; } i expecting method return same value passed in since i'm multiplying 100 , dividing 100 . . . bringing me original value yet when tested in compiler false. instance:
carinformation mycar = new carinformation(""); system.out.println(mycar.calc(34.86432456789)); i get:
34.0 why so?
after multiplication, cast int truncates beyond decimal point, 3486.432456789 becomes 3486, divided 100 34. it's overkill; no multiplication or division necessary, just: (int) amount. return value double, gets implicitly converted double: 34.0.
Comments
Post a Comment