This site uses strictly necessary cookies. More Information

X- 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?!?!?

**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
```

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

$$anonymous$$ight 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
```

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.

### Follow this Question

### Related Questions

Distribute terrain in zones 3 Answers

Unity seems to round small numbers to zero 2 Answers

Object slows down when moving to a NavPoint, speed is not constant 1 Answer

NullReference 1 Answer

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