# division

• 05-31-2006, 02:46 PM
wasssu
division
I'm using this:
Code:

```NumberFormat formatter = new DecimalFormat("##.##"); double nota=(punctaj / nr_intrebari)*10; String notafinala=formatter.format(nota);```
where punctaj and nr_intrebari are int

Why nota or notafinala is 0 (zero) is the result of the division is not an integer?
For example, if punctaj=1 and nr_intrebari=3 then nota=0.0.Why? I want the result to be a double like 0,33.
• 05-31-2006, 08:02 PM
masher
Your trouble is with this line:
Code:

`double nota=(punctaj / nr_intrebari)*10;`
Because punctaj and nr_intrebari are int's, then 1/3 = 0 -> you're doing integer division. Integer division discards the remainder. 1/3 = 0.333... so the integer answer is 0. 6/5 = 1.2 so the integer answer is 1.

If you want the result as a double, you have to cast.

Try this:
Code:

`double nota=(punctaj / (double)nr_intrebari)*10;`
The (double) tells the compiler that you want to evaluate the integer division as if it were floating point division.