Saturday 15 June 2013

java - Decreasing an int by a percentage? -



java - Decreasing an int by a percentage? -

i 99% done program, 1 part need decrease value (an int) 90%. thought oh can multiply 0.1 , it. can't because value beingness decreased 90% still needs int after decreased 90%.

this have, doesn't work reason stated above.

s2.increasealtitude(0.1*(s2.getaltitude()));

so problem don't know how go decreasing value 90% having remain integer.

the easiest way cast result of operation int:

s2.increasealtitude((int)(0.1*(s2.getaltitude())));

but round result downwards since casting double int truncate decimal part.

if want/need round or down, utilize math.round():

s2.increasealtitude((int)(math.round(0.1*(s2.getaltitude()))));

java int double percentage

No comments:

Post a Comment