60
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?

Sotirios Delimanolis
  • 252,278
  • 54
  • 635
  • 683
kiran
  • 639
  • 2
  • 6
  • 5

6 Answers6

105

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);
Jesper
  • 186,095
  • 42
  • 296
  • 332
unwind
  • 364,555
  • 61
  • 449
  • 578
21

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;
Peter Lawrey
  • 498,481
  • 72
  • 700
  • 1,075
  • Changing 100 to 100.0 did the trick, why is that? I'm using double. – felippe May 28 '16 at 13:20
  • 1
    @luizfelippe a common mistake is to do an integer operation and cast the result to a double or float. You need to perform the operation as a double i.e. `(double) (a / b)` when `a` and `b` are integers, is not the same as `(double) a / b` You want the later. – Peter Lawrey May 28 '16 at 20:46
5

(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.

Dheeresh
  • 113
  • 2
  • 7
3

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

Paŭlo Ebermann
  • 68,531
  • 18
  • 138
  • 203
1

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

System.out.println(new DecimalFormat("##.##").format(yourDouble) + "%"));
Zach Janicki
  • 64
  • 1
  • 3
1
String.format("%2.02f", (float)totalOptCount/totalRespCount);
Dalmas
  • 25,439
  • 9
  • 62
  • 75
  • were does it store , in which variable – kiran Feb 08 '11 at 10:34
  • Look at unwind's code, it's better explained. My code was just to tell you how to do it in one line, you can store the result in a variable if you want – Dalmas Feb 08 '11 at 10:36