# integer divisions always get 0

When larger value divided by small value is ok. But when small value is divided by Larger value becomes always 0.

Integer a = 123;

Integer b = 300;

Dobule result = 0.0;

result = (a/b) * 100;

For that above method, whenever I make division always getting 0.

Any idea?

Do like below -

``````Double a = 123;

Integer b = 300;

Double result = 0.0;

result = (a/b) * 100;
``````

at least a or b must be declared as Double. if one is Double then result of (a/b) be in double and you will get correct value. if both are Integer then (a/b) produce integer result in you case 0 and final result would be 0.

If both operands are Integer types, then your '/' yields the integer division operation, not the floating-point/decimal division you were expecting here. It doesn't matter that your lefthand assignee is a Decimal, because the division operation occurs before the assignment (division has higher biding power than assignment). Basically, in integer division, the decimal component of the result (i.e. the remainder) gets lopped off.

It works this way in C and Java too; it's not an Apex quirk (for once). While it's not bad that Apex is following the tradition of its syntactic predecessors, the intended usefulness of integer division -- processing efficiency -- serves no purpose in a high-overhead language like Apex. Nobody is going to be writing scientific computing algorithms or 3D graphics engines in Apex.

If you're using a literal in an expression, then the easy way to ensure a decimal division is to add a '.0' suffix, e.g. Decimal foo = 1.0/2; // foo gets assigned 0.5