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
Post a Comment