- Home /

# why is this divide calculation returning me 0?

```
float Percentage ;
int CountFalsed = 0;
int Falsed = 0;
Percentage = ( (1-0) /121) /1; // means if everything is true I'm giving 0.008% to each 121 slot if it's falsed I'm giving more %
Debug.Log("" + Percentage);
```

I went so far to the obvious

```
Percentage = (1/2);
```

returned me same 0

but when I give it

```
Percentage = (2/1);
```

returns me 2 at last ....

```
Percentage = (2*0.5);
```

gives me 1 at last

what's the calculation I do not understand here?!?!?

pixel_fiend

River-Ofarrows

Laniemme

chrysu76

$$anonymous$$

Hellium

gigazelle

CrandellWS

### People who like this

**Answer** by Bunny83
·
Mar 05, 2013 at 08:07 PM

Because you divide with integer values so it's an integer division. An integer division always returns an integer. the fractional part is always trucated. So 1 / 2 = 0.5 -> 0

You need to use float values. When you divide by constant numbers use a float literal:

```
int V = 10;
float P = V / 100.0f; // P will be 0.1f
```

If you want to divide two integers and want a fractional part you have to cast one of the operands into a float:

```
int V = 10;
int D = 100;
float P1 = V / D; // P1 will be 0.0f
float P2 = (float)V / D; // P2 will be 0.1f
```

Gbr

elegost

huloo

rus89

christianstrang

TooManySugar

pixel_fiend

River-Ofarrows

Laniemme

chrysu76

OreoSplitter

TheSwiftStrike

$$anonymous$$

Westland

And 21 more...

### People who like this

wow didn't know that thanks now I know why somewhere else I got some problems but it somehow worked but not as I wanted

In case someone else misses this (like I did) - If you're doing:

```
image.fillAmount = (intA / intB);
```

To make it work it needs to be:

```
image.fillAmount = ((float)intA / intB);
```

Might be other versions, still learning

**Answer** by gregroberts
·
Dec 01, 2014 at 08:22 PM

This worked for me:

```
int A = 10;
int B = 100;
float P1 = A / B; // P1 will be 0 int
float P2 = (A * 1.0f / B); // P2 will be 0.1f
```

### Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. **Please note, Unity Answers is now in read-only so we can prepare for the final data migration.**

For more information and updates, please read our full announcement thread in the Unity Forum.

### Follow this Question

### Related Questions

Distribute terrain in zones 3 Answers

wired calculation I don't get it 30-30 = 1.907349E-06 2 Answers

NullReference 1 Answer