If you are using int
values, using a double
may be a better choice and have less rounding error. float
can represent int
values without error up to ~16 million. double
can accurately represent all int
values.
double percentage =(double) totalOptCount / totalRespCount;
Percentages are usually multiplied by 100, meaning you can drop the cast.
double percentage = 100.0 * totalOptCount / totalRespCount;