java - Dividing long by long returns 0 -


this question has answer here:

i trying calculate % of used diskspace in windows , totaldrive denotes total diskspace of c drive in long , freedrive dentoes free space in long.

 long totaloccupied = totaldrive - freedrive; 

here calculating % of usage

 long percentageused =(totaloccupied/totaldrive*100);  system.out.println(percentageused); 

the print statement returns 0. can not getting desired value

you dividing long long, refers (long/long = long) operation, giving long result (in case 0).

you can achieve same thing casting either operand of division float type.

long percentageused = (long)((float)totaloccupied/totaldrive*100); 

Comments

Popular posts from this blog

image - ClassNotFoundException when add a prebuilt apk into system.img in android -

I need to import mysql 5.1 to 5.5? -

Java, Hibernate, MySQL - store UTC date-time -