Why does the division of two integers return 0.0 in Java?

JavaFloating PointInteger Division

Java Problem Overview


int totalOptCount = 500;
int totalRespCount=1500; 
float percentage =(float)(totalOptCount/totalRespCount);

Why does this always return value 0.0? Also I want to format this into 00.00 format and convert into string?

Java Solutions


Solution 1 - Java

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);

Solution 2 - Java

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;

Solution 3 - Java

>(totalOptCount/totalRespCount)

here both dividend and divisor are of type int which means they will allow only integer values and the answer of such equation will always be an integer literal.

if I break this it will be something like below

>(double)(500/1500)

According to the actual calculation, 500/1500 will give you 0.33333 but compiler will convert this into integer literal because both operands are of type int

>(double)(0)


Compiler gets an instruction to cast this 0 value to double so you got 0.0 as result

>0.0

and then you can change the result to any format as suggeted by @Zach Janicki.

keep in mind if both the operands are of same type than result will be of same type too.

Solution 4 - Java

Integer division (which includes long, short, byte, char, int) in Java always returns an int (or long, if one of the parameters is long), rounding towards zero. Your conversion occurs after this calculation.

(The formatting question is already answered by the other answers - alternatively you could also have a look at java.text.NumberFormat, specially java.text.DecimalFormat.)

Solution 5 - Java

String.format("%2.02f", (float)totalOptCount/totalRespCount);

Solution 6 - Java

to format a double and print out as a percentage, you can use use

System.out.println(new DecimalFormat("##.##").format(yourDouble) + "%"));

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionkiranView Question on Stackoverflow
Solution 1 - JavaunwindView Answer on Stackoverflow
Solution 2 - JavaPeter LawreyView Answer on Stackoverflow
Solution 3 - JavaDheereshView Answer on Stackoverflow
Solution 4 - JavaPaŭlo EbermannView Answer on Stackoverflow
Solution 5 - JavaDalmasView Answer on Stackoverflow
Solution 6 - JavaZach JanickiView Answer on Stackoverflow