Help with calculating percentage in C
-
Hope no one screams at me but if they do I guess its okay this question is simple its ridiculous but here goes my total number in decimal is 1,621,270,208 the number I am trying to figure out the percent is from is 1,604,667,016 I did this calculation on the web and it came out to 98% the two number the first being total is a int the second unalloc is also a int. the variable percent is a double or float I thought mistaking that percent = unalloc / total woudgive me 0000.98 in percent but I realize that since the quotient is zero percent is 000.000 doing percent = unalloc % total would give the reminder in percent and thus a value of 1604665335.000 I have searched the web I know this is real simple to some of you wonder if you could help thanks in advance
-
Hope no one screams at me but if they do I guess its okay this question is simple its ridiculous but here goes my total number in decimal is 1,621,270,208 the number I am trying to figure out the percent is from is 1,604,667,016 I did this calculation on the web and it came out to 98% the two number the first being total is a int the second unalloc is also a int. the variable percent is a double or float I thought mistaking that percent = unalloc / total woudgive me 0000.98 in percent but I realize that since the quotient is zero percent is 000.000 doing percent = unalloc % total would give the reminder in percent and thus a value of 1604665335.000 I have searched the web I know this is real simple to some of you wonder if you could help thanks in advance
Presumably you're doing something like
int total = 100;
int unalloc = 96;
double percent = unalloc/total;So the problem here is that since unaloc and total are both
int
, the calculation is done in integer math, and then promoted to double. What you need to do is to promote either operand to the division to double (or both):double percent = (double)unalloc/total:
Keep Calm and Carry On
-
Presumably you're doing something like
int total = 100;
int unalloc = 96;
double percent = unalloc/total;So the problem here is that since unaloc and total are both
int
, the calculation is done in integer math, and then promoted to double. What you need to do is to promote either operand to the division to double (or both):double percent = (double)unalloc/total:
Keep Calm and Carry On
-
Hope no one screams at me but if they do I guess its okay this question is simple its ridiculous but here goes my total number in decimal is 1,621,270,208 the number I am trying to figure out the percent is from is 1,604,667,016 I did this calculation on the web and it came out to 98% the two number the first being total is a int the second unalloc is also a int. the variable percent is a double or float I thought mistaking that percent = unalloc / total woudgive me 0000.98 in percent but I realize that since the quotient is zero percent is 000.000 doing percent = unalloc % total would give the reminder in percent and thus a value of 1604665335.000 I have searched the web I know this is real simple to some of you wonder if you could help thanks in advance
double total = 1621270208;
double percentFrom = 1604667016;
double percent = percentFrom / total * 100; -
Presumably you're doing something like
int total = 100;
int unalloc = 96;
double percent = unalloc/total;So the problem here is that since unaloc and total are both
int
, the calculation is done in integer math, and then promoted to double. What you need to do is to promote either operand to the division to double (or both):double percent = (double)unalloc/total:
Keep Calm and Carry On
hey k5054 my comment does not relate to the currently running thread. I just wanted to let you know I keep getting back to some of your replies in topic I have posted quite a while ago. I thought it might be a good idea to let you know I`m haunting those posts.
-
double total = 1621270208;
double percentFrom = 1604667016;
double percent = percentFrom / total * 100; -
hey k5054 my comment does not relate to the currently running thread. I just wanted to let you know I keep getting back to some of your replies in topic I have posted quite a while ago. I thought it might be a good idea to let you know I`m haunting those posts.