# Can't produce results percentage out of my class

I have this class, but the results for perLost and PerWon is always 0, I can't figure out what I am doing wrong.

`````` public List<Opportunity> Stages {get; private set;}
public Integer counterLost {get;private set;}
public Integer counterWon {get;private set;}
public Integer counterRTS {get;private set;}
public Integer Sum {get;private set;}
public Double perLost {get;private set;}
public Double perWon {get;private set;}
> public ClosedLostStages() {   Stages = [ Select Id , StageName , Amount
> , Name , Owner.Name
>              FROM Opportunity
>              WHERE   StageName = 'Closed Lost'];
>
>  counterLost = [ Select count()
>                     FROM Opportunity
>                   WHERE   StageName = 'Closed Lost' ];
>
>  counterWon = [ Select count()
>                     FROM Opportunity
>                   WHERE   StageName = 'Closed Won' ];
>
>   Sum = counterWon + counterLost ;    perLost = ((counterLost / Sum) *
> 100) ;    System.debug(perLost + 'Lost percentage');  perWon =
> ((counterWon / Sum) * 100) ;  System.debug(perWon + 'Won percentage');
``````

I'm assuming that counterLost, counterWon, perLost, Sum, and perWon are all Integer values. Integer division where the numerator is larger than the denominator will always result in a zero mid-calculation (e.g. counterWon/Sum, 25/100 is 0.25, cast as an Integer is 0, resulting in calculating 0 * 100, which is still 0).

To avoid this loss of precision, declare Sum as a Decimal instead of an Integer. This will force counterWon and counterLost to be promoted to a decimal during calculation, and the precision won't be lost. Alternatively, you could also cast Sum to a Decimal, but I like to make the code as legible as possible.

You'll need to get the Integer value back from the calculation if perWon and perLost are integer values. Like so:

``````Sum = counterWon + counterLost;
Integer perLost = ((counterLost / Sum) * 100).intValue();
System.debug(perLost + 'Lost percentage');
perWon = ((counterWon / Sum) * 100).intValue();
System.debug(perWon + 'Won percentage');
``````

Now with fully functional example that was written in my developer org:

``````public class ClosedLostStages {
public List<Opportunity> Stages {get; private set;}
public Integer counterLost {get;private set;}
public Integer counterWon {get;private set;}
public Integer counterRTS {get;private set;}
public Decimal Sum {get;private set;}
public Double perLost {get;private set;}
public Double perWon {get;private set;}

public ClosedLostStages() {
Stages = [Select Id, StageName, Amount, Name, Owner.Name
FROM Opportunity
WHERE   StageName = 'Closed Lost'];
counterLost = [ Select count()
FROM Opportunity
WHERE   StageName = 'Closed Lost' ];

counterWon = [ Select count()
FROM Opportunity
WHERE   StageName = 'Closed Won' ];

Sum = counterWon + counterLost ;
perLost = ((counterLost / Sum) * 100) ;
System.debug(perLost + 'Lost percentage');
perWon = ((counterWon / Sum) * 100) ;
System.debug(perWon + 'Won percentage');
}
}
``````
• this is the first part – Metawaa Mar 20 '15 at 20:58
• public List<Opportunity> Stages {get; private set;} public Integer counterLost {get;private set;} public Integer counterWon {get;private set;} public Integer counterRTS {get;private set;} public Integer Sum {get;private set;} public Double perLost {get;private set;} public Double perWon {get;private set;} – Metawaa Mar 20 '15 at 20:58
• that gives me error – Metawaa Mar 20 '15 at 21:44
• @bez What error? – sfdcfox Mar 21 '15 at 0:22
• (ClosedLostStages) Method does not exist or incorrect signature: [Integer].intValue() – Metawaa Mar 24 '15 at 15:40