Because the conversion to float happens after the division has been done. You need:
float percentage = ((float) totalOptCount) / totalRespCount;
You should be able to format using something like:
String str = String.format("%2.02f", percentage);
More Related Contents:
- Why does integer division code give the wrong answer? [duplicate]
- Division of integers in Java [duplicate]
- Integer division: How do you produce a double?
- Retain precision with double in Java
- How many significant digits do floats and doubles have in java?
- Convert float to double without losing precision
- How can a primitive float value be -0.0? What does that mean?
- Why does Double.NaN==Double.NaN return false?
- comparing float/double values using == operator
- Manipulating and comparing floating points in java
- What is the difference between the float and integer data type when the size is the same?
- Double calculation producing odd result [duplicate]
- why f is placed after float values?
- Why converting from float to double changes the value?
- Do any JVM’s JIT compilers generate code that uses vectorized floating point instructions?
- How can I handle precision error with float in Java?
- Why does this subtraction not equal zero?
- Why does changing the sum order returns a different result?
- Division between integers in Java
- What is the inclusive range of float and double in Java?
- Why does dividing a float by an integer return 0.0?
- How do Java runtimes targeting pre-SSE2 processors implement floating-point basic operations?
- How do I check if a zero is positive or negative?
- Java double precision sum trouble
- Are the bit patterns of NaNs really hardware-dependent?
- Why is comparing floats inconsistent in Java?
- How to convert IEEE-11073 16-bit SFLOAT to simple float in Java?
- Java double comparison epsilon
- How to convert hex string to float in Java?
- What is float in Java?