+ Start a Discussion
dat le ducdat le duc 

Sum of two picklist values in Visualforce Page and show the total

Hi ,
I have three fields in my VF page and user has to choose value from two of them manually.
Once both the values are choosed,  its SUM will be showed dynamically on the screen, it's ok . 
The problem is the sum result is a String that i don't want.
Selected values : 1 and 2  ー> result : 12
Expected result : 3

How to convert selected values to number type before sum total?
Please Help.

Thanks!!       
<apex:page standardController="EvaluationInformation__c" extensions="EISControllerV">
.....
<!--Picklist (values : 1,2)-->
<div>
	<apex:inputField id="Emp_1_1" value="{!evaluation.PE1}" styleClass="plStyle"/>
	<apex:actionSupport event="onchange" rerender="sumwrapper"></apex:actionSupport>
</div>   
<div>
	<apex:inputField id="Emp_1_2" value="{!evaluation.PE2}" styleClass="plStyle"/>
	<apex:actionSupport event="onchange" rerender="sumwrapper"></apex:actionSupport>
</div>  

<!-- SUM will be showed here-->
<apex:outputPanel id="sumwrapper">
	<apex:outputText value="{!evaluation.PE1 + evaluation.PE2}" id="dpdresult" />      
</apex:outputPanel>
.....
</apex:page>

 
Best Answer chosen by dat le duc
Vishwajeet kumarVishwajeet kumar
Hello,
One way could be to write method which can convert both picklist values into integer, sum and return it. This method's returned data can be used as value on apex:outputText.

Extension Controller Method : 

public Integer  getSumOfPicklistValues(){
      return Integer.valueOf(evaluation.PE1) + Integer.valueOf(evaluation.PE2);
}

Page update : 
<!-- SUM will be showed here-->
<apex:outputPanel id="sumwrapper">
      <apex:outputText value="{!SumOfPicklistValues}" id="dpdresult" />
</apex:outputPanel>


Thanks

All Answers

Vishwajeet kumarVishwajeet kumar
Hello,
One way could be to write method which can convert both picklist values into integer, sum and return it. This method's returned data can be used as value on apex:outputText.

Extension Controller Method : 

public Integer  getSumOfPicklistValues(){
      return Integer.valueOf(evaluation.PE1) + Integer.valueOf(evaluation.PE2);
}

Page update : 
<!-- SUM will be showed here-->
<apex:outputPanel id="sumwrapper">
      <apex:outputText value="{!SumOfPicklistValues}" id="dpdresult" />
</apex:outputPanel>


Thanks
This was selected as the best answer
dat le ducdat le duc