Strange division error in Java when calculating percentage -


i'm getting strange results while trying calculate percentages. if have value of 1, , max value of 2, should 50% result (because 1 50% of 2), instead i'm getting 1% result. here code:

int percent = ( runningmins / goal.getgoalmins() )  * 100; system.out.println(runningmins +", " + goal.getgoalmins() + ", progress: " + percent); 

result when runningmins = 1 , goal.getmins = 1:

1, 1, progress: 100 

result when runningmins = 1 , goal.getmins = 2:

1, 2, progress: 1 

runningmins = 1 , goal.getmins = 4:

1, 4, progress: 1 

runningmins = 1 , goal.getmins = 8:

1, 8, progress: 1 

what doing wrong here?

you try :

int percent = ( (float)runningmins / goal.getgoalmins() )  * 100; 

or better yet

int percent = ( 100 * runningmins ) / goal.getgoalmins() 

because dividing integers cannot give else 0 or 1...


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -

php - Accessing static methods using newly created $obj or using class Name -